-
Notifications
You must be signed in to change notification settings - Fork 20
Phantom variance ZSTs #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
what if they were named |
|
we could just use pub struct PhantomData<T: ?Sized>;
pub struct PhantomArg<T: ?Sized>(PhantomData<fn(T)>);
pub struct PhantomInvariant<T: ?Sized>(PhantomData<(fn(T), T)>);
pub struct PhantomLifetime<'a>(PhantomData<&'a ()>);
pub struct PhantomArgLifetime<'a>(PhantomArg<&'a ()>);
pub struct PhantomInvariantLifetime<'a>(PhantomInvariant<&'a ()>); |
I'd prefer that they will get some language support, so to instantiate one I can write e.g. |
Of course As to whether they should be lang items from the start, it is forward compatible to not do this initially. That's not to mention that it's a libs ACP, though T-lang could certainly weigh in unofficially if they wanted. |
We discussed this in the libs-api meeting. We felt that this is useful to have and would make code clearer to read. However we would prefer names closer to the 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) -> T>);
pub struct PhantomCovariantLifetime<'a'>(PhantomCovariant<&'a ()>);
pub struct PhantomInvariantLifetime<'a'>(PhantomInvariant<&'a ()>);
pub struct PhantomContravariantLifetime<'a'>(PhantomContravariant<&'a ()>); We felt that having explicit names with the concept variance was the clearest terminology. Someone writing unsafe code should read the documentation for these types which clearly explains the implications of each of these kinds of variance. The |
I'll update the issue to this effect.
That's certainly reasonable.
(assuming you mean Is this also desired? I can look into how |
Couldn't they just be type aliases to allow unit construction? I think it would still be strongly-typed enough with the type parameter. |
you can't construct using a type alias unless you use struct literal syntax ( |
@Amanieu Given the specific design was brought up in the team meeting, is this considered accepted? I ask because it wasn't explicitly stated (and closed) as is typically the case. |
Given that we proposed quite a significant change from what was proposed, we wanted to give you a change to respond if you had any feedback. But otherwise yes, you can consider this accepted. |
What I originally proposed was far closer; I had adjusted it based on feedback. The only difference was that I originally wanted to uplift the crate directly (such that |
Implement phantom variance markers ACP accepted rust-lang/libs-team#488 Tracking issue rust-lang#135806
Implement phantom variance markers ACP accepted rust-lang/libs-team#488 Tracking issue rust-lang#135806
Rollup merge of rust-lang#135807 - jhpratt:phantom-variance, r=Amanieu Implement phantom variance markers ACP accepted rust-lang/libs-team#488 Tracking issue rust-lang#135806
Implement phantom variance markers ACP accepted rust-lang/libs-team#488 Tracking issue rust-lang#135806
Proposal
Problem statement
Expressing the intended variance of a generic type or lifetime is possible but difficult to understand without additional documentation.
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 thetype_variance
crate.All deriveable traits will be implemented. The
Debug
implementation should output the variance ofT
usingcore::any::type_name
.Alternatives
core
.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
type_variance
crateWhat 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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: