-
Notifications
You must be signed in to change notification settings - Fork 34
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
feature: const variants of into, from, try_from, and default #147
Open
cosmicexplorer
wants to merge
7
commits into
illicitonion:main
Choose a base branch
from
cosmicexplorer:const-into
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04dc34d
add ConstIntoPrimitive
cosmicexplorer 6cd542d
add ConstFromPrimitive
cosmicexplorer 5ac2ed7
add ConstTryFromPrimitive
cosmicexplorer 88ce96f
add ConstDefault
cosmicexplorer 14d31b9
fix unnecessary `as`
cosmicexplorer f2798c2
move *Into* logic into common helper methods
cosmicexplorer c49b885
impl IntoPrimitive when ConstIntoPrimitive is selected
cosmicexplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![allow(non_upper_case_globals)] | ||
|
||
// Guard against https://github.com/illicitonion/num_enum/issues/27 | ||
mod alloc {} | ||
mod core {} | ||
mod num_enum {} | ||
mod std {} | ||
|
||
#[test] | ||
fn default() { | ||
#[derive(Debug, Eq, PartialEq, ::num_enum::ConstDefault)] | ||
#[repr(u8)] | ||
enum Enum { | ||
#[allow(unused)] | ||
Zero = 0, | ||
#[num_enum(default)] | ||
NonZero = 1, | ||
} | ||
|
||
const nz: Enum = Enum::const_default(); | ||
assert_eq!(Enum::NonZero, nz); | ||
} | ||
|
||
#[test] | ||
fn default_standard_default_attribute() { | ||
#[derive(Debug, Eq, PartialEq, ::num_enum::ConstDefault)] | ||
#[repr(u8)] | ||
enum Enum { | ||
#[allow(unused)] | ||
Zero = 0, | ||
#[default] | ||
NonZero = 1, | ||
} | ||
|
||
const nz: Enum = Enum::const_default(); | ||
assert_eq!(Enum::NonZero, nz); | ||
} |
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,116 @@ | ||
#![allow(non_upper_case_globals)] | ||
|
||
use ::num_enum::ConstFromPrimitive; | ||
|
||
// Guard against https://github.com/illicitonion/num_enum/issues/27 | ||
mod alloc {} | ||
mod core {} | ||
mod num_enum {} | ||
mod std {} | ||
|
||
macro_rules! has_from_primitive_number { | ||
( $type:ty ) => { | ||
paste::paste! { | ||
#[test] | ||
fn [<has_from_primitive_number_ $type>]() { | ||
#[derive(Debug, Eq, PartialEq, ConstFromPrimitive)] | ||
#[repr($type)] | ||
enum Enum { | ||
Zero = 0, | ||
#[num_enum(default)] | ||
NonZero = 1, | ||
} | ||
|
||
const zero: Enum = Enum::const_from(0 as $type); | ||
assert_eq!(zero, Enum::Zero); | ||
|
||
const one: Enum = Enum::const_from(1 as $type); | ||
assert_eq!(one, Enum::NonZero); | ||
|
||
const two: Enum = Enum::const_from(2 as $type); | ||
assert_eq!(two, Enum::NonZero); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
has_from_primitive_number!(u8); | ||
has_from_primitive_number!(u16); | ||
has_from_primitive_number!(u32); | ||
has_from_primitive_number!(u64); | ||
has_from_primitive_number!(usize); | ||
has_from_primitive_number!(i8); | ||
has_from_primitive_number!(i16); | ||
has_from_primitive_number!(i32); | ||
has_from_primitive_number!(i64); | ||
has_from_primitive_number!(isize); | ||
// repr with 128-bit type is unstable | ||
// has_from_primitive_number!(u128); | ||
// has_from_primitive_number!(i128); | ||
|
||
#[test] | ||
fn has_from_primitive_number_standard_default_attribute() { | ||
#[derive(Debug, Eq, PartialEq, ConstFromPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero = 0, | ||
#[default] | ||
NonZero = 1, | ||
} | ||
|
||
const zero: Enum = Enum::const_from(0_u8); | ||
assert_eq!(zero, Enum::Zero); | ||
|
||
const one: Enum = Enum::const_from(1_u8); | ||
assert_eq!(one, Enum::NonZero); | ||
|
||
const two: Enum = Enum::const_from(2_u8); | ||
assert_eq!(two, Enum::NonZero); | ||
} | ||
|
||
#[test] | ||
fn from_primitive_number_catch_all() { | ||
#[derive(Debug, Eq, PartialEq, ConstFromPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero = 0, | ||
#[num_enum(catch_all)] | ||
NonZero(u8), | ||
} | ||
|
||
const zero: Enum = Enum::const_from(0_u8); | ||
assert_eq!(zero, Enum::Zero); | ||
|
||
const one: Enum = Enum::const_from(1_u8); | ||
assert_eq!(one, Enum::NonZero(1_u8)); | ||
|
||
const two: Enum = Enum::const_from(2_u8); | ||
assert_eq!(two, Enum::NonZero(2_u8)); | ||
} | ||
|
||
#[cfg(feature = "complex-expressions")] | ||
#[test] | ||
fn from_primitive_number_with_inclusive_range() { | ||
#[derive(Debug, Eq, PartialEq, ConstFromPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero = 0, | ||
#[num_enum(alternatives = [2..=255])] | ||
NonZero, | ||
} | ||
|
||
const zero: Enum = Enum::const_from(0_u8); | ||
assert_eq!(zero, Enum::Zero); | ||
|
||
const one: Enum = Enum::const_from(1_u8); | ||
assert_eq!(one, Enum::NonZero); | ||
|
||
const two: Enum = Enum::const_from(2_u8); | ||
assert_eq!(two, Enum::NonZero); | ||
|
||
const three: Enum = Enum::const_from(3_u8); | ||
assert_eq!(three, Enum::NonZero); | ||
|
||
const twofivefive: Enum = Enum::const_from(255_u8); | ||
assert_eq!(twofivefive, Enum::NonZero); | ||
} |
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,52 @@ | ||
#![allow(non_upper_case_globals)] | ||
|
||
use ::num_enum::ConstIntoPrimitive; | ||
|
||
// Guard against https://github.com/illicitonion/num_enum/issues/27 | ||
mod alloc {} | ||
mod core {} | ||
mod num_enum {} | ||
mod std {} | ||
|
||
#[derive(ConstIntoPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero, | ||
One, | ||
Two, | ||
} | ||
|
||
#[test] | ||
fn simple() { | ||
const zero: u8 = Enum::Zero.const_into(); | ||
assert_eq!(zero, 0u8); | ||
assert_eq!(zero, Enum::Zero.into()); | ||
|
||
const one: u8 = Enum::One.const_into(); | ||
assert_eq!(one, 1u8); | ||
assert_eq!(one, Enum::One.into()); | ||
|
||
const two: u8 = Enum::Two.const_into(); | ||
assert_eq!(two, 2u8); | ||
assert_eq!(two, Enum::Two.into()); | ||
} | ||
|
||
#[test] | ||
fn catch_all() { | ||
#[derive(Debug, Eq, PartialEq, ConstIntoPrimitive)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Zero = 0, | ||
#[num_enum(catch_all)] | ||
NonZero(u8), | ||
} | ||
|
||
const zero: u8 = Enum::Zero.const_into(); | ||
assert_eq!(zero, 0u8); | ||
|
||
const one: u8 = Enum::NonZero(1u8).const_into(); | ||
assert_eq!(one, 1u8); | ||
|
||
const two: u8 = Enum::NonZero(2u8).const_into(); | ||
assert_eq!(two, 2u8); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you feel about making
ConstTryFromPrimitive
a subtrait ofTryFromPrimitive
(and similarly for the others)? I think that would allow us to removeConstTryFromPrimitiveError
(we could just useTryFromPrimitiveError
as the bound would still hold), and would mean the new const generic onEnumInfo
could disappear?I guess it's a bit annoying to have to derive both if you just want the const version, but I'm also not sure how common it would be to only want the const version, and also it mirrors how things like
PartialEq
/Eq
work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds great! It reminds me of what I was trying to do with
ConstantTimePartialOrd
insubtle
a while ago (dalek-cryptography/subtle#98). I'll see if this works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally do not have a use case for const-only derives -- I think I was a little confused about how
const fn
can also be a normal runtimefn
and how to separate those. I think a subtrait and merging the errors (which I would definitely like to do) would be ideal -- will check that out now.