Type Definition phantasm::Invariant

source · []
pub type Invariant<T: ?Sized> = Invariant<T>;
Expand description

Marker zero-sized type that is invariant over T.

“Invariant” means that given F<_>, Super and Sub (where Sub is a subtype of Super), F<Sub> is not a subtype of F<Super> and vice versa - F<Super> is not a subtype of F<Sub>

Examples

use phantasm::Invariant;

// This struct is invariant over `T`
struct Test<T>(Invariant<T> /* ... */);

let _: Test<i32> = Test(Invariant /* ... */);
let _ = Test::<i32>(Invariant /* ... */);
let _ = Test(Invariant::<i32> /* ... */);
use phantasm::{Invariant, Lt};

// `F<Sub>` is **not** a subtype of `F<Super>`
fn covariant_fail<'l>(with_sub: Invariant<Lt<'static>>) {
    let with_super: Invariant<Lt<'l>> = with_sub; // mismatched types
}
use phantasm::{Invariant, Lt};

// `F<Super>` is **not** a subtype of `F<Sub>`
fn contravariant_fail<'l>(with_super: Invariant<Lt<'l>>) {
    let with_sub: Invariant<Lt<'static>> = with_super; // mismatched types
}

See also