Fixed up some of the code for c_enums and c_flags. Also added examples.
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ macro_rules! c_flags
|
||||
}
|
||||
else
|
||||
{
|
||||
Some($name {bits: bits})
|
||||
Some($name { bits: bits })
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,13 +53,13 @@ macro_rules! c_flags
|
||||
#[inline]
|
||||
pub fn empty() -> $name
|
||||
{
|
||||
$name {bits: 0}
|
||||
$name { bits: 0 }
|
||||
}
|
||||
|
||||
/// Returns the set containing all flags.
|
||||
pub fn all() -> $name
|
||||
{
|
||||
$name {bits: $($flag.bits)|+}
|
||||
$name { bits: $($flag.bits)|+ }
|
||||
}
|
||||
|
||||
/// Returns `true` if no flags are currently stored;
|
||||
@ -122,7 +122,7 @@ macro_rules! c_flags
|
||||
/// Returns the union of the two sets of flags.
|
||||
fn bitor(self, other: $name) -> $name
|
||||
{
|
||||
$name {bits: self.bits | other.bits}
|
||||
$name { bits: self.bits | other.bits }
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,7 +133,7 @@ macro_rules! c_flags
|
||||
/// Returns the left flags, but with all the right flags toggled.
|
||||
fn bitxor(self, other: $name) -> $name
|
||||
{
|
||||
$name {bits: self.bits ^ other.bits}
|
||||
$name { bits: self.bits ^ other.bits }
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ macro_rules! c_flags
|
||||
/// Returns the intersection between the two sets of flags.
|
||||
fn bitand(self, other: $name) -> $name
|
||||
{
|
||||
$name {bits: self.bits & other.bits}
|
||||
$name { bits: self.bits & other.bits }
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ macro_rules! c_flags
|
||||
/// Returns the set difference of the two sets of flags.
|
||||
fn sub(self, other: $name) -> $name
|
||||
{
|
||||
$name {bits: self.bits & !other.bits}
|
||||
$name { bits: self.bits & !other.bits }
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@ macro_rules! c_flags
|
||||
/// Returns the complement of this set of flags.
|
||||
fn not(self) -> $name
|
||||
{
|
||||
$name {bits: !self.bits} & $name::all()
|
||||
$name { bits: !self.bits } & $name::all()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user