Fixed up some of the code for c_enums and c_flags. Also added examples.

This commit is contained in:
Jason Smith
2017-08-24 14:02:53 -04:00
parent 8b5d5a9c57
commit d408d4b239
4 changed files with 96 additions and 29 deletions

View File

@ -22,6 +22,8 @@ macro_rules! c_enum
$($(#[$variantAttribute])* $variant = $value,)*
}
impl $name
{
/// Generate a variant of the enum from a given value.
@ -30,22 +32,12 @@ macro_rules! c_enum
/// to not use this in a high performance loop.
pub fn from_value(val: $fieldType) -> Option<$name>
{
// This would be better as a match statement, but
// unfortunately, it needs to be a giant set of if
// statements since we only have the expression type
// to work with, not a pattern type.
/*
match val
{
$($value => {Some($name::$variant)})*
$($value => { Some($name::$variant) })*
_ => {None}
_ => { None }
}
*/
$(if val == $value {return Some($name::$variant);})*
// No variant was found.
None
}
/// 'true', if the given value matches a variant
@ -54,15 +46,9 @@ macro_rules! c_enum
{
match $name::from_value(val)
{
Some(_) =>
{
true
}
Some(_) => { true }
None =>
{
false
}
None => { false }
}
}
@ -89,7 +75,7 @@ macro_rules! c_enum
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "{}", self.to_str())
::std::fmt::Display::fmt(self, f)
}
}