[−][src]Crate boolean_enums
Generate your Yes/No enum with gen_boolean_enum!
:
gen_boolean_enum!(MyEnum);
It's From
let flag = MyEnum::Yes; let oflag = true.into(); assert_eq!(flag, oflag); if (!flag).into() { unreachable!() }
To generate a public enum, you need to append pub
to
the macro arguments:
gen_boolean_enum!(pub MyEnum);
You can serialize and deserialize it with serde like a normal bool
(enabled by the serde
feature). For that, specify serde
before the enum name in gen_boolean_enum!
:
#[macro_use] extern crate boolean_enums; extern crate toml; // as an example serde format gen_boolean_enum!(serde MyEnum); // or gen_boolean_enum!(pub serde MyEnum); #[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] struct SomeStruct { flag: MyEnum } // … let first = SomeStruct { flag: MyEnum::Yes }; let string = toml::ser::to_string(&first).unwrap(); let second: SomeStruct = toml::de::from_str(&string).unwrap(); assert_eq!(first, second);
You can use boolean-enums in no_std
crates by disabling the default std
feature:
[dependencies.boolean-enums]
version = "^0.3.0"
default-features = false
Examples
#[macro_use] extern crate boolean_enums; gen_boolean_enum!(First); gen_boolean_enum!(Second); gen_boolean_enum!(Third); fn do_smth(flag1: First, flag2: Second, flag3: Third) { // … } let first = First::Yes; let second = Second::No; let third = Third::Yes; do_smth(first, second, third);
That compiles perfectly, but
ⓘThis example deliberately fails to compile
do_smth(first, third, second);
fails to compile.
Macros
gen_boolean_enum | Generates enum with Yes and No variants. |