Trait from different versions, why it works?

Let's say I have crates: A, B and main.
A depends on crate mega_traits 0.1.1
B depends on crate mega_traits 0.2.1
main depends on A and B and mega_traits 0.2.1.

In A there is type:

struct A;
impl mega_traits::Trait for A {}

and in main

fn f<X: mega_traits::Trait>(x: X) {}
f(A::A);

and this code compiles and works without problelm. Why?
Cargo.lock for main contains both version of mega_traits.
So A::A implements mega_traits@0.1.1::Trait,
while main::f requires mega_traits@0.2.1::Trait.
So I expect compile time error here,
but it works. But how? Is compiler smart enough to check mega_traits@0.1.1::Trait and mega_traits@0.2.1::Trait find out that this exactly the same trait and allow usage of
mega_traits@0.1.1::Trait instead of mega_traits@0.2.1::Trait ?

Semver trick enables it.
For example, num-traits 0.1.43 re-exports traits in num-traits 0.2.

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.