-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cbeb44
commit 889fd3e
Showing
6 changed files
with
611 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Unreleased | ||
|
||
- Support for #[repr(type)] | ||
- Support for Other variant | ||
|
||
# Version 1.3.0 (2024-07-15) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ use unit_enum::UnitEnum; | |
enum Color { | ||
Red = 10, | ||
Green, | ||
Blue = 45654 | ||
Blue = 45654, | ||
} | ||
|
||
fn main() { | ||
|
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,41 @@ | ||
use unit_enum::UnitEnum; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, UnitEnum)] | ||
#[repr(u32)] | ||
enum Color { | ||
Red = 10, | ||
Green, | ||
Blue = 45654, | ||
|
||
#[unit_enum(other)] | ||
Other(u32), | ||
} | ||
|
||
fn main() { | ||
// Get the name of a variant | ||
assert_eq!(Color::Blue.name(), "Blue"); | ||
|
||
// Get the ordinal (position) of a variant | ||
assert_eq!(Color::Green.ordinal(), 1); | ||
|
||
// Convert from ordinal back to variant | ||
assert_eq!(Color::from_ordinal(2), Some(Color::Blue)); | ||
assert_eq!(Color::from_ordinal(4), None); | ||
|
||
// Get the discriminant value (respects the repr type) | ||
assert_eq!(Color::Blue.discriminant(), 45654); | ||
assert_eq!(Color::Green.discriminant(), 11); | ||
|
||
// Convert from discriminant back to variant | ||
assert_eq!(Color::from_discriminant(10), Color::Red); | ||
assert_eq!(Color::from_discriminant(0), Color::Other(0)); | ||
|
||
// Get the total number of variants | ||
assert_eq!(Color::len(), 3); | ||
|
||
// Iterate over all variants | ||
assert_eq!( | ||
Color::values().collect::<Vec<_>>(), | ||
vec![Color::Red, Color::Green, Color::Blue] | ||
); | ||
} |
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.