[src]

Struct std::kinds::marker::CovariantType

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:

use std::cast;

struct S<T> { x: *() }
fn get<T>(s: &S<T>) -> T {
   unsafe {
       let x: *T = cast::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<~[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

Derived Implementations

impl<T: Clone> Clone for CovariantType<T>

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

Returns a copy of the value. The contents of owned pointers are copied to maintain uniqueness, while the contents of managed pointers are not copied.

fn clone_from(&mut self, source: &Self)

Perform copy-assignment from source.

a.clone_from(&b) is equivalent to a = b.clone() in functionality, but can be overridden to reuse the resources of a to avoid unnecessary allocations.

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

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

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