You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
discriminant_value intrinsic -- tracking issue for 639 #24263
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCP-mediumMedium priorityT-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
RE the documentation issue: The value wouldn't be unstable for the case of a C-like enum, so the statement about the value being unstable should be qualified as such. And, it would also be useful to document that this feature can/should be used instead of as for safer conversions of C-like enums. A new lint that does that would be great.
However, the RFC says that the type is always u64 which means that I'd have to use as in my code anyway, as I need u8-typed discriminant values (e.g. for parsing ASN.1). Accordingly, I think that the part of the design where the return type is fixed at u64 should be fixed.
This trait would be auto-implemented for every enum and Repr defined to be the type of the discriminant (in case the discriminant is packed away, Repr is the smallest integer type with enough bits). Also, Repr corresponds to the signedness of the enum in the case of things like #[repr(i8)] which makes it easier to compare discriminants for ordering.
For Stylo we want to build with a stable Rust compiler. For Servo’s style crate, the only thing missing is discriminant_value. (servo/servo#11816)
We use it with an enum that represents a property declaration: there’s one variant per supported CSS property, and each variants has a field with the property value. (The Rust types for values can be different for each property.) During the cascade, we get an sequence of declarations ordered by precedence, and we want to only keep one per property. We use discriminant_value to determine which property a given enum value represents.
There’s already some code generation going on in this crate, so we could write a function or method that behaves similarly to discriminant_value and uses match internally, but I don’t know if this would optimize as well. (This is used in a hot loop.)
@SimonSapin if the results of the internal function are exactly the same as discriminant_value, LLVM should be able to optimise away the match completely and replace it with the same memory load that would be performed by discriminant_value. This has worked for me with several C-like enums (hand-checking the assembly output), so this might be a viable alternative if you do not want to wait for the stabilisation. Nonetheless I would recommend to verify if LLVM optimisations also work as intended in your case.
has the signature of the intrinsic gotten a bound, as listed as a work item in the description by Niko?
my understanding is that the answer here is "no."
should we provide a stable function that is merely a shim over the existing intrinsic, or should we hold off stabilization in order to attempt to implement some way to vary the return type based on the particular enum (which has been discussed previously, including in the comments here).
I'm personally ambivalent here; I can see the motivation to stabilize the thing we have, but I also respect people concerned about prematurely stabilizing before exploring the alternatives.
if we decide to hold off stabilization to explore an alternative API: do such alternatives require a new RFC (or at least RFC amendment)?
In theory we can change that since the docs say the return value for
non-enums is unspecified. But an enum constructor is "just" a function, so
I'm not sure it's a good idea.
On Thu, Sep 7, 2017 at 12:50 PM, Taylor Cramer ***@***.***> wrote:
I know this issue has already been closed, and the function is on track
for stabilization, but I thought I should bring this up anyway since it
seems odd to me: mem::discriminant returns 0 for all non-enums, *including
enum constructors*. That is, the following code prints 0 and then 1:
use std::mem;
enum MyEnum {
First,
Second(i32)
}
fn main() {
println!("{:?}", mem::discriminant(&MyEnum::Second)); // prints `0`
println!("{:?}", mem::discriminant(&MyEnum::Second(2))); // prints `1`
}
IMO, it'd be nice to have the discriminant of the enum constructor match
the discriminant of the enum value. I'm not sure if I've missed the boat
here, but this seems like it would be really valuable for getting the
discriminant without having to fully construct a value (this doesn't work
for struct constructors, but that's something we could revisit).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#24263 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n5_4DIAuM3s6HQ4nvkuUTBhPsIcKks5sgB66gaJpZM4D96D5>
.
Hmm. I agree with @durka that at first it surprised me (because I think of them as a function), though really the type of a variant is (I believe...) a zero-sized type that uniquely identifies the enum variant. (Unless of course you coerce it into a function.) So it seems like it should be possible to make it work, and I do find @cramertj's use case compelling.
@nikomatsakis We employ the nuclear option (Foo::Bar is a function which you call with braces instead of parens and any function can be defined like that - named arguments!).
We didn't ever close this conversation, but mem::discriminant is set to stabilize in 1.21. Can we at least adjust the docs to say that the return value for non-enums is unspecified?
@cramertj I'm not sure what you're asking for -- it currently says "If T is
not an enum, calling this function will not result in undefined behavior,
but the return value is unspecified."
The current Discriminant<T> contains a PhantomData<*const T>. This means it's non-Send and non-Sync, but I can't think of a good reason it should be. Is there one?
Hullo, does anybody know if any discussion/work is ongoing in line with @cramertj 's suggestion regarding calling std::mem::discriminant on an enum "constructor"?
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCP-mediumMedium priorityT-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
Activity
pnkfelix commentedon Apr 10, 2015
The prototype implementation at #20907 is very easy to rebase (I have already done so). I plan to land that prototype as part of work towards #15523
Implement `discriminant_value` intrinsic
pyfisch commentedon Apr 26, 2015
How is the progress here? I want to use it in a package, but I don't think it will be 1.0, right? Will it be included in 1.1?
briansmith commentedon Mar 6, 2016
RE the documentation issue: The value wouldn't be unstable for the case of a C-like enum, so the statement about the value being unstable should be qualified as such. And, it would also be useful to document that this feature can/should be used instead of
asfor safer conversions of C-like enums. A new lint that does that would be great.However, the RFC says that the type is always
u64which means that I'd have to useasin my code anyway, as I needu8-typed discriminant values (e.g. for parsing ASN.1). Accordingly, I think that the part of the design where the return type is fixed atu64should be fixed.durka commentedon Jun 13, 2016
I previously suggested a way to solve the return type problem: introduce a trait instead of (or in addition to) the intrinsic function.
This trait would be auto-implemented for every enum and
Reprdefined to be the type of the discriminant (in case the discriminant is packed away,Repris the smallest integer type with enough bits). Also,Reprcorresponds to the signedness of the enum in the case of things like#[repr(i8)]which makes it easier to compare discriminants for ordering.SimonSapin commentedon Jun 21, 2016
For Stylo we want to build with a stable Rust compiler. For Servo’s
stylecrate, the only thing missing isdiscriminant_value. (servo/servo#11816)We use it with an enum that represents a property declaration: there’s one variant per supported CSS property, and each variants has a field with the property value. (The Rust types for values can be different for each property.) During the cascade, we get an sequence of declarations ordered by precedence, and we want to only keep one per property. We use
discriminant_valueto determine which property a given enum value represents.There’s already some code generation going on in this crate, so we could write a function or method that behaves similarly to
discriminant_valueand usesmatchinternally, but I don’t know if this would optimize as well. (This is used in a hot loop.)ranma42 commentedon Jun 22, 2016
@SimonSapin if the results of the internal function are exactly the same as
discriminant_value, LLVM should be able to optimise away the match completely and replace it with the same memory load that would be performed bydiscriminant_value. This has worked for me with several C-like enums (hand-checking the assembly output), so this might be a viable alternative if you do not want to wait for the stabilisation. Nonetheless I would recommend to verify if LLVM optimisations also work as intended in your case.SimonSapin commentedon Jun 22, 2016
These enums are not C-like though, otherwise I could use
enum_value as usize. I’ll try it anyway.pnkfelix commentedon Jun 23, 2016
My questions at this point:
enum(which has been discussed previously, including in the comments here).80 remaining items
durka commentedon Sep 7, 2017
nikomatsakis commentedon Sep 7, 2017
Hmm. I agree with @durka that at first it surprised me (because I think of them as a function), though really the type of a variant is (I believe...) a zero-sized type that uniquely identifies the enum variant. (Unless of course you coerce it into a function.) So it seems like it should be possible to make it work, and I do find @cramertj's use case compelling.
nikomatsakis commentedon Sep 7, 2017
But what about "struct" variants?
eddyb commentedon Sep 7, 2017
@nikomatsakis We employ the nuclear option (
Foo::Baris a function which you call with braces instead of parens and any function can be defined like that - named arguments!).cramertj commentedon Sep 7, 2017
@eddyb I suggested that exact thing on IRC in the lang channel:
stabilize mem::discriminant (closes rust-lang#24263)
cramertj commentedon Sep 21, 2017
We didn't ever close this conversation, but
mem::discriminantis set to stabilize in 1.21. Can we at least adjust the docs to say that the return value for non-enums is unspecified?durka commentedon Sep 21, 2017
cramertj commentedon Sep 21, 2017
@durka Ah, I was referring to your comment above where you said
durka commentedon Sep 21, 2017
We could try to head off the mistake by writing "If T is not an enum (including tuple variant constructors!)" or including your example from above.
joshtriplett commentedon Sep 21, 2017
@cramertj @durka Is there a fundamental reason we can't produce a compiler error if calling it on a non-enum (including an enum "constructor")?
durka commentedon Sep 21, 2017
@joshtriplett see previous discussion.
Twey commentedon Oct 3, 2017
The current
Discriminant<T>contains aPhantomData<*const T>. This means it's non-Sendand non-Sync, but I can't think of a good reason it should be. Is there one?sfackler commentedon Oct 3, 2017
Nope, we should impl those traits.
omaskery commentedon Apr 23, 2018
Hullo, does anybody know if any discussion/work is ongoing in line with @cramertj 's suggestion regarding calling std::mem::discriminant on an enum "constructor"?