-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UnsafeFromPrimitive ignores default/alternatives (#112)
* UnsafeFromPrimitive ignores default/alternatives Previously it would fail to derive, but there are valid use-cases for wanting to perform the unsafe conversion for known "natural" values. This potentially opens up a bit of confusion around what the behaviours are when these attributes are present but ignored, but these are also documented, and if someone is reaching for this unsafe function rather than using `{,Try}FromPrimitive` or just an `as` cast, presumably they're paying attention... * Deprecate from_unchecked, and rename it unchecked_transmute_from --------- Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
- Loading branch information
1 parent
9b4642e
commit e02abdc
Showing
11 changed files
with
145 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
num_enum/tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![deny(deprecated)] | ||
|
||
use num_enum::UnsafeFromPrimitive; | ||
|
||
#[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero, | ||
One, | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); | ||
assert_eq!(Enum::from_unchecked(1_u8), Enum::One); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
num_enum/tests/try_build/compile_fail/from_unchecked_deprecated_warning.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: use of deprecated associated function `num_enum::UnsafeFromPrimitive::from_unchecked`: Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release. | ||
--> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:14:26 | ||
| | ||
14 | assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:1:9 | ||
| | ||
1 | #![deny(deprecated)] | ||
| ^^^^^^^^^^ | ||
|
||
error: use of deprecated associated function `num_enum::UnsafeFromPrimitive::from_unchecked`: Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release. | ||
--> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:15:26 | ||
| | ||
15 | assert_eq!(Enum::from_unchecked(1_u8), Enum::One); | ||
| ^^^^^^^^^^^^^^ |
11 changes: 0 additions & 11 deletions
11
num_enum/tests/try_build/compile_fail/unpexpected_alternatives.rs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
num_enum/tests/try_build/compile_fail/unpexpected_alternatives.stderr
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
num_enum/tests/try_build/compile_fail/unpexpected_default.rs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
num_enum/tests/try_build/compile_fail/unpexpected_default.stderr
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.