Skip to content

Phantom variance ZSTs #488

Closed
Closed
@jhpratt

Description

@jhpratt

Proposal

Problem statement

Expressing the intended variance of a generic type or lifetime is possible but difficult to understand without additional documentation.

PhantomData<&'a ()> // covariant in 'a
PhantomData<fn(&'a ())> // contravariant in 'a
PhantomData<fn(&'a ()) -> &'a ()> // invariant in 'a

PhantomData<T> // covariant in T
PhantomData<fn(T)> // contravariant in T
PhantomData<fn(T) -> T> // invariant in T

Motivating examples or use cases

The previous types are used in a number of places, including in the standard library. Many such uses are not documented as to the meaning or intent.

Solution sketch

Add the following to core::marker, which is derived from the type_variance crate.

pub struct PhantomCovariant<T: ?Sized>(PhantomData<fn(T)>);
pub struct PhantomInvariant<T: ?Sized>(PhantomData<fn(T) -> T>);
pub struct PhantomContravariant<T: ?Sized>(PhantomData<fn(T)>);
pub struct PhantomCovariantLifetime<'a>(PhantomCovariant<&'a ()>);
pub struct PhantomInvariantLifetime<'a>(PhantomInvariant<&'a ()>);
pub struct PhantomContravariantLifetime<'a>(PhantomContravariant<&'a ()>);

// later trait impl(self) Variance {}, utilizing restrictions
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 {}

All deriveable traits will be implemented. The Debug implementation should output the variance of T using core::any::type_name.

Alternatives

  • Leave this as a crate and don't include it in core.
  • Create new lang items (or use a hack similar to ghost) to permit constructing the values directly. This does not need to be done now, as starting with the above is forward compatible with later making it a lang item.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions