Open
Description
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
- ACP: Phantom variance ZSTs libs-team#488
- Implementation: Implement phantom variance markers #135807
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Should there be a single
Lifetime
type or one for each possible variance? The former would mean writingPhantomCovariant<Lifetime<'a>>
instead ofPhantomCovariantLifetime<'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?