Skip to content

Need some way to retrieve expected enum discriminant for comparison #4017

Open

Description

@crlf0710

There's currently no way to retrieve and store std::mem::Discriminant without explicitly construct an enum value first. However preparing such an enum value might be nontrivial or even have side effects.

I propose for variants defined as constructors (with parenthesis), we could support retrieving the discriminant somehow, directly.

Strawman proposal:

Add a macro called constructor_discriminant (or some better name).

Make this piece of code usable:

enum OutputType {
    StdOut(std::io::StdOut),
    StdErr(std::io::StdErr),
}

let v = OutputType::StdOut(std::io::stdout());
assert_eq!(std::mem::discriminant(v), constructor_discriminant!(OutputType::StdOut));

Activity

  1. kennytm commented on Oct 11, 2026

    @kennytm
    Member

    This is basically the discriminant_of! macro proposed in #3727.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions