Struct std::kinds::marker::CovariantTypeExperimental [-]  [+] [src]

pub struct CovariantType<T>;

A marker type whose type parameter T is considered to be covariant with respect to the type itself. This is (typically) used to indicate that an instance of the type T is being stored into memory and read from, even though that may not be apparent.

For more information about variance, refer to this Wikipedia article http://en.wikipedia.org/wiki/Variance_%28computer_science%29.

Note: It is very unusual to have to add a covariant constraint. If you are not sure, you probably want to use InvariantType.

Example

Given a struct S that includes a type parameter T but does not actually reference that type parameter:

fn main() { use std::mem; struct S<T> { x: *() } fn get<T>(s: &S<T>) -> T { unsafe { let x: *T = mem::transmute(s.x); *x } } }
use std::mem;

struct S<T> { x: *() }
fn get<T>(s: &S<T>) -> T {
   unsafe {
       let x: *T = mem::transmute(s.x);
       *x
   }
}

The type system would currently infer that the value of the type parameter T is irrelevant, and hence a S<int> is a subtype of S<Box<int>> (or, for that matter, S<U> for any U). But this is incorrect because get() converts the *() into a *T and reads from it. Therefore, we should include the a marker field CovariantType<T> to inform the type checker that S<T> is a subtype of S<U> if T is a subtype of U (for example, S<&'static int> is a subtype of S<&'a int> for some lifetime 'a, but not the other way around).

Trait Implementations

impl<T> Copy for CovariantType<T>

impl<T> Clone for CovariantType<T>

fn clone(&self) -> CovariantType<T>

fn clone_from(&mut self, &CovariantType<T>)

Derived Implementations

impl<T: Ord> Ord for CovariantType<T>

fn cmp(&self, __arg_0: &CovariantType<T>) -> Ordering

impl<T: PartialOrd<T>> PartialOrd<CovariantType<T>> for CovariantType<T>

fn partial_cmp(&self, __arg_0: &CovariantType<T>) -> Option<Ordering>

fn lt(&self, __arg_0: &CovariantType<T>) -> bool

fn le(&self, __arg_0: &CovariantType<T>) -> bool

fn gt(&self, __arg_0: &CovariantType<T>) -> bool

fn ge(&self, __arg_0: &CovariantType<T>) -> bool

fn lt(&self, &CovariantType<T>) -> bool

fn le(&self, &CovariantType<T>) -> bool

fn gt(&self, &CovariantType<T>) -> bool

fn ge(&self, &CovariantType<T>) -> bool

impl<T: Eq> Eq for CovariantType<T>

fn assert_receiver_is_total_eq(&self)

impl<T: PartialEq<T>> PartialEq<CovariantType<T>> for CovariantType<T>

fn eq(&self, __arg_0: &CovariantType<T>) -> bool

fn ne(&self, __arg_0: &CovariantType<T>) -> bool

fn ne(&self, &CovariantType<T>) -> bool