pub type Covariant<T: ?Sized> = Covariant<T>;
Expand description
Marker zero-sized type that is covariant over T
.
“Covariant” means that given F<_>
, Super
and Sub
(where Sub
is a
subtype of Super
), F<Sub>
is a subtype of F<Super>
(but F<Super>
is not a subtype of F<Sub>
)
Examples
use phantasm::Covariant;
// This struct is covariant over `T`
struct Test<T>(Covariant<T> /* ... */);
let _: Test<i32> = Test(Covariant /* ... */);
let _ = Test::<i32>(Covariant /* ... */);
let _ = Test(Covariant::<i32> /* ... */);
use phantasm::{Covariant, Lt};
// `F<Sub>` **is** a subtype of `F<Super>`
fn covariant_pass<'l>(with_sub: Covariant<Lt<'static>>) {
let with_super: Covariant<Lt<'l>> = with_sub;
}
ⓘ
use phantasm::{Covariant, Lt};
// `F<Super>` is **not** a subtype of `F<Sub>`
fn contravariant_fail<'l>(with_super: Covariant<Lt<'l>>) {
let with_sub: Covariant<Lt<'static>> = with_super; // mismatched types
}
See also
- crate docs
PhantomData
- Subtyping and Variance nomicon chapter