Struct core::kinds::marker::ContravariantTypeExperimental [-]  [+] [src]

pub struct ContravariantType<T: ?Sized>;

A marker type whose type parameter T is considered to be contravariant with respect to the type itself. This is (typically) used to indicate that an instance of the type T will be consumed (but not 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 contravariant 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: *const () } fn get<T>(s: &S<T>, v: T) { unsafe { let x: fn(T) = mem::transmute(s.x); x(v) } } }
use std::mem;

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

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 fn(T) and then passes a value of type T to it.

Supplying a ContravariantType marker would correct the problem, because it would mark S so that S<T> is only a subtype of S<U> if U is a subtype of T; given that the function requires arguments of type T, it must also accept arguments of type U, hence such a conversion is safe.

Trait Implementations

impl<T: ?Sized> Copy for ContravariantType<T>

impl<T: ?Sized> Clone for ContravariantType<T>

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

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

Derived Implementations

impl<T: Ord + ?Sized> Ord for ContravariantType<T>

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

impl<T: PartialOrd + ?Sized> PartialOrd for ContravariantType<T>

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

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

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

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

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

impl<T: Eq + ?Sized> Eq for ContravariantType<T>

impl<T: PartialEq + ?Sized> PartialEq for ContravariantType<T>

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

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