Skip to content

discriminant_value intrinsic -- tracking issue for 639 #24263

Closed

Description

@nikomatsakis

Tracking issue for rust-lang/rfcs#639.

Some things to nail down before stabilization:

  • prototype implementation
  • signature needs to have a <T:Reflect> bound
  • documentation should reflect the inherent instability in this value and describe valid uses
  • perhaps signature should require input T to actually be an enum instance?
  • do we want to adjust the return type in any way to enforce the above rules?

Activity

  1. pnkfelix commented on Apr 10, 2015

    @pnkfelix
    Contributor

    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

  2. added
    B-RFC-approved
    on Apr 10, 2015
  3. pyfisch commented on Apr 26, 2015

    @pyfisch
    Contributor

    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?

  4. added
    T-lang
    T-libs-api
    on Aug 11, 2015
  5. added
    B-unstable
    and removed
    B-RFC-approved
    on Feb 18, 2016
  6. briansmith commented on Mar 6, 2016

    @briansmith
    Contributor

    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.

  7. durka commented on Jun 13, 2016

    @durka
    Contributor

    I previously suggested a way to solve the return type problem: introduce a trait instead of (or in addition to) the intrinsic function.

    trait MagicEnumTrait {
        type Repr;
        fn value(&self) -> Self::Repr;
    }

    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.

  8. SimonSapin commented on Jun 21, 2016

    @SimonSapin
    Contributor

    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.)

  9. ranma42 commented on Jun 22, 2016

    @ranma42
    Contributor

    @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.

  10. SimonSapin commented on Jun 22, 2016

    @SimonSapin
    Contributor

    These enums are not C-like though, otherwise I could use enum_value as usize. I’ll try it anyway.

  11. pnkfelix commented on Jun 23, 2016

    @pnkfelix
    Contributor

    My questions at this point:

    • 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)?
      • I think the answer here is "yes."
  12. 80 remaining items

  13. durka commented on Sep 7, 2017

    @durka
    Contributor
  14. nikomatsakis commented on Sep 7, 2017

    @nikomatsakis
    ContributorAuthor

    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.

  15. nikomatsakis commented on Sep 7, 2017

    @nikomatsakis
    ContributorAuthor

    But what about "struct" variants?

    enum Foo { ... Bar { ... } }`
  16. eddyb commented on Sep 7, 2017

    @eddyb
    Contributor

    @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!).

  17. cramertj commented on Sep 7, 2017

    @cramertj
    Member

    @eddyb I suggested that exact thing on IRC in the lang channel:

    fn foo { a: i32, b: i32, c: i32 } -> i32 { a + b + c }
    
    assert_eq!(foo { a: 1, b: 2, c: 3 }, 6);
  18. added a commit that references this issue on Sep 14, 2017
    c4474ab
  19. cramertj commented on Sep 21, 2017

    @cramertj
    Member

    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?

  20. durka commented on Sep 21, 2017

    @durka
    Contributor
  21. cramertj commented on Sep 21, 2017

    @cramertj
    Member

    @durka Ah, I was referring to your comment above where you said

    In theory we can change that since the docs say the return value for
    non-enums is unspecified.

  22. durka commented on Sep 21, 2017

    @durka
    Contributor

    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.

  23. joshtriplett commented on Sep 21, 2017

    @joshtriplett
    Member

    @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")?

  24. durka commented on Sep 21, 2017

    @durka
    Contributor
  25. Twey commented on Oct 3, 2017

    @Twey
    Contributor

    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?

  26. sfackler commented on Oct 3, 2017

    @sfackler
    Member

    Nope, we should impl those traits.

  27. omaskery commented on Apr 23, 2018

    @omaskery

    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"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-RFC-implementedB-unstableC-tracking-issueP-mediumT-langT-libs-apifinal-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions