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.