Merge branch 'master' of gitlab.com:CyberMages/Core/binding
This commit is contained in:
commit
622808879f
47
examples/enums.rs
Normal file
47
examples/enums.rs
Normal file
@ -0,0 +1,47 @@
|
||||
#[macro_use]
|
||||
extern crate binding;
|
||||
|
||||
|
||||
|
||||
c_enum!
|
||||
{
|
||||
/// Test enum is a simple enum testing the
|
||||
/// capabilities of creating a C enum.
|
||||
enum TestEnum : i32
|
||||
{
|
||||
/// This is the first item.
|
||||
variant One = 1,
|
||||
|
||||
/// This is the second item.
|
||||
variant Two = -2,
|
||||
|
||||
/// This is the third item.
|
||||
variant Three = 3
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let cenum: TestEnum;
|
||||
|
||||
cenum = TestEnum::One;
|
||||
println!("Enum value is: {}", cenum);
|
||||
|
||||
if TestEnum::is_valid_value(-2) == true
|
||||
{
|
||||
match TestEnum::from_value(-2)
|
||||
{
|
||||
Some(variant) =>
|
||||
{
|
||||
println!("Enum value is: {}", variant.to_value());
|
||||
}
|
||||
|
||||
None =>
|
||||
{
|
||||
panic!("Should not happen since we checked ahead of time.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
examples/flags.rs
Normal file
34
examples/flags.rs
Normal file
@ -0,0 +1,34 @@
|
||||
#[macro_use]
|
||||
extern crate binding;
|
||||
|
||||
|
||||
|
||||
c_flags!
|
||||
{
|
||||
/// A sample of flags useful for the EvDev library.
|
||||
flags ReadFlags: u32
|
||||
{
|
||||
/// Process data in sync mode.
|
||||
const SYNC = 0b000000000000000000000001,
|
||||
|
||||
/// Process data in normal mode.
|
||||
const NORMAL = 0b000000000000000000000010,
|
||||
|
||||
/// Pretend the next event is a SYN_DROPPED and require
|
||||
/// the caller to sync.
|
||||
const FORCE_SYNC = 0b000000000000000000000100,
|
||||
|
||||
/// The fd is not in O_NONBLOCK and a read may block.
|
||||
const BLOCKING = 0b000000000000000000001000
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let flag: ReadFlags;
|
||||
|
||||
flag = SYNC | NORMAL;
|
||||
println!("Flag: {:#010b}", flag.get_bits());
|
||||
println!("Flag: {}", flag);
|
||||
}
|
@ -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) })*
|
||||
|
||||
_ => { 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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user