Feature gate: #![feature(phantom_variance_markers)]
This is a tracking issue for phantom variance markers, which are identical to PhantomData but provide a self-documenting variance rather than contrived types such as PhantomData<fn() -> T> to indicate contravariance.
Public API
// in core::marker
pub struct PhantomCovariant<T: ?Sized>(/* ... */);
pub struct PhantomInvariant<T: ?Sized>(/* ... */);
pub struct PhantomContravariant<T: ?Sized>(/* ... */);
pub struct PhantomCovariantLifetime<'a>(/* ... */);
pub struct PhantomInvariantLifetime<'a>(/* ... */);
pub struct PhantomContravariantLifetime<'a>(/* ... */);
pub trait Variance: Sealed {}
impl<T: ?Sized> Variance for PhantomCovariant<T> {}
impl<T: ?Sized> Variance for PhantomInvariant<T> {}
impl<T: ?Sized> Variance for PhantomContravaiant<T> {}
impl<T: ?Sized> Variance for PhantomCovariantLifetime<T> {}
impl<T: ?Sized> Variance for PhantomInvariantLifetime<T> {}
impl<T: ?Sized> Variance for PhantomContravaiantLifetime<T> {}
pub fn variance<T: Variance>() -> T {}
// also the trait impls you would expect; identical to PhantomData
Steps / History
Unresolved Questions
- Should there be a single
Lifetime type or one for each possible variance? The former would mean writing PhantomCovariant<Lifetime<'a>> instead of PhantomCovariantLifetime<'a>, with the API otherwise being identical.
- Should the values be directly constructable, eliminating the need for
::new()? This would presumably mean adding 4–6 lang items, depending on the previous bullet point.
- Are we happy exposing the (unstable) rustc_pub_transparent on these, allowing indirect inclusion in repr(transparent) types?
Feature gate:
#![feature(phantom_variance_markers)]This is a tracking issue for phantom variance markers, which are identical to
PhantomDatabut provide a self-documenting variance rather than contrived types such asPhantomData<fn() -> T>to indicate contravariance.Public API
Steps / History
Unresolved Questions
Lifetimetype or one for each possible variance? The former would mean writingPhantomCovariant<Lifetime<'a>>instead ofPhantomCovariantLifetime<'a>, with the API otherwise being identical.::new()? This would presumably mean adding 4–6 lang items, depending on the previous bullet point.Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩