Crate boolean_enums[][src]

Generate your Yes/No enum with gen_boolean_enum!:

gen_boolean_enum!(MyEnum);

It's From and Into and Not:

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, first add serde_derive and interpolate_idents to your Cargo.toml dependencies. Then specify serde before the enum name in gen_boolean_enum!:

This example is not tested
// required by the macro implementation
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate serde_derive;

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);

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) {
    // …
}

fn main() {
    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_common

Implementation detail

_gen_boolean_enum_gen_enum

Implementation detail

_gen_boolean_enum_serde

Implementation detail

gen_boolean_enum

Generates enum with Yes and No variants.