Crates.io | type-variance |
lib.rs | type-variance |
version | 0.1.0 |
source | src |
created_at | 2020-07-19 17:42:31.027218 |
updated_at | 2020-09-01 13:40:20.159142 |
description | Marker traits for subtype variance |
homepage | |
repository | https://gitlab.com/nwn/variance |
max_upload_size | |
id | 266940 |
size | 17,467 |
Variance is a set of PhantomData
-like marker types that make it easier to
specify the variance of your generic types with respect to their parameters.
The Variance crate is available on crates.io. Add the following dependency to your Cargo manifest:
[dependencies]
type-variance = "0.1.0"
See the docs for detailed usage information.
The crate provides three zero-sized marker types: Covariant<T>
,
Contravariant<T>
, and Invariant<T>
, each marking the given type parameter
as having the respective variance.
For example:
use type_variance::{Covariant, Contravariant};
// UnaryFunction is a zero-sized type that is covariant to `Arg` and
// contravariant to `Ret`.
struct UnaryFunction<Arg, Ret> {
arg: Covariant<Arg>,
ret: Contravariant<Ret>,
}
fn foo<'sup>() {
// Here, the type &'static() is a subtype of &'sup().
// Therefore, `Arg` may be replaced with a subtype and `Ret` may be
// replaced with a supertype.
let _func: UnaryFunction<&'sup(), &'static()> = UnaryFunction {
arg: Covariant::<&'static()>::default(),
ret: Contravariant::<&'sup()>::default(),
};
}
This crate is MIT licensed.