type-variance 0.1.0

Marker traits for subtype variance
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented3 out of 7 items with examples
  • Size
  • Source code size: 24.98 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nwn/variance
    0 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nwn

Variance

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.

Latest version Documentation License

Getting Started

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.

Example

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(),
    };
}

License

This crate is MIT licensed.