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

Add a trait for IntoPrimitive #156

Open
jgcodes2020 opened this issue Oct 27, 2024 · 2 comments
Open

Add a trait for IntoPrimitive #156

jgcodes2020 opened this issue Oct 27, 2024 · 2 comments

Comments

@jgcodes2020
Copy link

FromPrimitive and TryFromPrimitive already have traits, why not IntoPrimitive?

@illicitonion
Copy link
Owner

We could add one if it's useful. IIRC we added the others so that we could do blanket implementations of other traits. If we wanted to add a blanket implementation using IntoPrimitive it'd probably be a semver break (as it could cause impl conflicts for whatever we added blanket implementations of).

Do you have a concrete use-case you'd like to use IntoPrimitive for, or is this more of an aesthetic question?

@jgcodes2020
Copy link
Author

I'm using this crate with bindgen to generate enums that play nice with Rust. Bindgen generates enums as #[repr(u32)] or #[repr(i32)] on Linux depending on whether the enum has negative values; but on Windows all enums are #[repr(i32)]. I already use TryFromPrimitive to safely cast to enums, but theoretically, having an IntoPrimitive trait allows you to do this, regardless of what the enum's primitive type is. Unlike val as u16, this is checked.

fn mess_with_enum(val: MyEnum) -> u16 {
  let int_val = val.into_primitive();
  u16::try_from(int_val).unwrap();
}

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

No branches or pull requests

2 participants