Skip to content
New issue

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

Expose enum value as integer #178

Closed
dtolnay opened this issue May 1, 2020 · 0 comments · Fixed by #182
Closed

Expose enum value as integer #178

dtolnay opened this issue May 1, 2020 · 0 comments · Fixed by #182

Comments

@dtolnay
Copy link
Owner

dtolnay commented May 1, 2020

#170 produces "enums" with a private integer representation, and public associated constants corresponding to the variants.

#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub enum SmallPrime(u32);

#[allow(non_upper_case_globals)]
impl SmallPrime {
    pub const Two: Self = SmallPrime(2);
    pub const Three: Self = SmallPrime(3);
    pub const Five: Self = SmallPrime(6);
}

This behaves like an ordinary Rust enum in all ways except that it enforces a wildcard match arm when matched (which is deliberate) and that it cannot be cast using as to a primitive (which is unfortunate).

We should expose some way to get the primitive value. Maybe a method, maybe just a straightforward public field like n.repr.

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

Successfully merging a pull request may close this issue.

1 participant