There is also this other very lengthy and detailed post I have written quite recently: Looking for a deeper understanding of PhantomData - #4 by Yandros
In there, I show a macro to test subtyping (and thus variance):
macro_rules! const_assert_subtypes {(
for [<$($generics:tt)*]
$T1:ty : $($T2:tt)*
) => (
const _: () = {
fn foo<$($generics)* (x: *const $T1)
-> *const $($T2)*
{
x
}
};
)}
trait Trait<'lifetime> {}
const_assert_subtypes!(
for [<'short, 'long : 'short>]
dyn Trait<'long> : dyn Trait<'short>
);
// Same for dyn Trait<'short> : dyn Trait<'long>
// And for impl Trait
- All the assertions fail: Playground