We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
For example, given an enum like this
enum Minituple { Zero, One(i32), Two(i32, i32), More(Vec<i32>), }
I would like to have a function that converts each variant to a unique integer, like this:
impl Minituple { pub fn into_integer(&self) -> usize { match self { Zero => 0, One(_) => 1, Two(_, _) => 2, More(_) => 3, } } }
Considerations:
IntoInteger
IntoUsize
IntoReprType
The text was updated successfully, but these errors were encountered:
You can use the EnumDiscriminants feature and cast with as to the primitive number type you want:
EnumDiscriminants
as
#[derive(EnumDiscriminants)] enum Minituple { Zero, One(i32), Two(i32, i32), More(Vec<i32>), } impl Minituple { fn into_integer(&self) -> usize { let discriminant: MinitupleDiscriminants = self.into(); discriminant as usize // Alternatives: // - `Into::<MinitupleDiscriminants>::into(self) as usize` // - `MinitupleDiscriminants::from(self) as usize` } } // ... // Outputs `discriminant: 2` println!("discriminant: {}", Minituple::Two(42, 24).into_integer());
So technically, what you'd want is instead a method like as_discriminant()? You could then follow with as usize, or impl your own method to wrap that?
as_discriminant()
as usize
impl
Sorry, something went wrong.
No branches or pull requests
For example, given an enum like this
I would like to have a function that converts each variant to a unique integer, like this:
Considerations:
IntoInteger
? MaybeIntoUsize
andIntoReprType
seprarately?The text was updated successfully, but these errors were encountered: