-
Notifications
You must be signed in to change notification settings - Fork 18
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
impl TryFrom<T::Type> for BitFlags<T> #14
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de54d59
Add fmt::Debug constraint to BitFlagNum
arcnmx 9eb16f3
impl TryFrom<T::Type> for BitFlags<T>
arcnmx e3cf71f
move try_from_bits and FromBitsError to fallible module
arcnmx 8861464
Simplify the impl_try_from macro
meithecatte 8499f0c
fix std::error::Error impl
arcnmx 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,62 @@ | ||
use core::convert::TryFrom; | ||
use core::fmt; | ||
use super::BitFlags; | ||
use super::_internal::RawBitFlags; | ||
|
||
macro_rules! impl_try_from { | ||
($($ty:ty),*) => { | ||
$( | ||
impl<T> TryFrom<$ty> for BitFlags<T> | ||
where | ||
T: RawBitFlags<Type=$ty>, | ||
{ | ||
type Error = FromBitsError<T>; | ||
|
||
fn try_from(bits: T::Type) -> Result<Self, Self::Error> { | ||
let flags = Self::from_bits_truncate(bits); | ||
if flags.bits() == bits { | ||
Ok(flags) | ||
} else { | ||
Err(FromBitsError { | ||
flags, | ||
invalid: bits & !flags.bits(), | ||
}) | ||
} | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
impl_try_from! { | ||
u8, u16, u32, u64, usize | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct FromBitsError<T: RawBitFlags> { | ||
flags: BitFlags<T>, | ||
invalid: T::Type, | ||
} | ||
|
||
impl<T: RawBitFlags> FromBitsError<T> { | ||
pub fn truncate(self) -> BitFlags<T> { | ||
self.flags | ||
} | ||
|
||
pub fn invalid_bits(self) -> T::Type { | ||
self.invalid | ||
} | ||
} | ||
|
||
impl<T: RawBitFlags + fmt::Debug> fmt::Display for FromBitsError<T> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
write!(fmt, "Invalid bits for {:?}: {:#b}", self.flags, self.invalid) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<T: RawBitFlags + fmt::Debug> std::error::Error for FromBitsError<T> { | ||
fn description(&self) -> &str { | ||
"invalid bitflags representation" | ||
} | ||
} |
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
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
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.
Since this is a public API, it'd be nice to have a sentence or two of documentation here. You could even write a doctest which tries to convert a number with some invalid flags and then asserts about the results of
truncate
andinvalid_bits
. This would kill two birds with one stone, since there aren't really any tests for this functionality right now.