Make discriminant value public

Input

I run this code:

use std::mem::{discriminant, Discriminant};

fn main() {
    let health_status = Status::Healthy;

    let Discriminant(discriminant) = discriminant(&health_status);

    dbg!(&discriminant);
}

#[derive(Debug, Clone)]
pub enum Status {
    Healthy = 0,
    Unhealthy = 1,
    Reserved = 2,
}


Output

and I get this CLI output:

error[E0532]: cannot match against a tuple struct which contains private fields
 --> src/main.rs:6:9
  |
6 |     let Discriminant(discriminant) = discriminant(&health_status);
  |         ^^^^^^^^^^^^
  |
note: constructor is not visible here due to private fields
 --> src/main.rs:6:22
  |
6 |     let Discriminant(discriminant) = discriminant(&health_status);
  |                      ^^^^^^^^^^^^ private field

For more information about this error, try `rustc --explain E0532`.
error: could not compile `nice` (bin "nice") due to 1 previous error

Request:

make the discriminant field public.

Motivation:

I'm trying to pass an enum as a function argument instead of u8, because the variant name serves as an explanation for what the number means. But once I passed the enum as an argument, the function still needs to turn the enum into a number (0, 1 or 2) before using it. Right now I'd use as casting. But as casting can do a lot of different things in the background so it's not a safe approach for extracting the discriminant. Making the discriminant value public would solve the issue.

as casting does nothing special in the <enum> as <integer> case. You can use it just fine.

For the fieldless case, use use as as was mentioned.

For a broader conversation, see Add an expression for direct access to an enum's discriminant by scottmcm · Pull Request #3607 · rust-lang/rfcs · GitHub