-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Deprecate the FromPrimitive
trait
#16920
Comments
This is also used for converting from primitives to enums via deriving. I think it would be weird for this to be a freestanding non-trait impl created by deriving. |
I have unsafe code that uses What's the idiomatic way to do this conversion? Write a giant |
Same question as @toh-ableton - What's the new recommended way for converting an integer to a C style enum? |
Since nobody else seems to have a good workaround, I just published the enum_primitive crate, which exports an #[macro_use] extern crate enum_primitive;
extern crate num;
use num::FromPrimitive;
enum_from_primitive! {
#[derive(Debug, PartialEq)]
enum FooBar {
Foo = 17,
Bar = 42,
}
}
fn main() {
assert_eq!(FooBar::from_i32(17), Some(FooBar::Foo));
assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar));
assert_eq!(FooBar::from_i32(91), None);
} |
@andersk Wow, thanks so much for doing that! We had been looking into an alternative derive mode that didn't require the traits as a stop-gap, but this is a better way to go for now. Ultimately, I think we want a more first-class way to handle these conversions and talk about enum size, etc, but that will take some time. |
/cc @rust-lang/libs |
This was actually removed (yay!), so closing. |
internal: Fix new nightly clippy lints
internal: Fix new nightly clippy lints
Two reasons:
Option
, even when the function cannot returnNone
. The standard library is full of calls followed byunwrap
.Examples:
FromPrimitive
can ever returnNone
forBigInt
None
forBigUint
FromPrimitive::<i8>::from_i32
can returnNone
because of overflow, whileFromPrimitive::<f64>::from_i32
cannot.The only problem I see is in
libtest/stats.rs
, whereFromPrimitive
is used generically. I think however that we can figure it out.I think the best solution would be have different implementations for each type.
cc @aturon
The text was updated successfully, but these errors were encountered: