Struct core::kinds::marker::CovariantTypeExperimental
[-]
[+]
[src]
pub struct CovariantType<T: ?Sized>;
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::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).