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