Skip to content

Commit

Permalink
added custom display example using flags_iter config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas3674 committed Oct 22, 2024
1 parent 51b598d commit 4d12b19
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bitmask_enum::bitmask;

use std::fmt;

#[bitmask(u8)]
#[bitmask_config(flags_iter)]
enum Bitmask {
Flag1, // defaults to 0d00000001
Flag2, // defaults to 0d00000010
Flag3, // defaults to 0d00000100
}

impl fmt::Display for Bitmask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(Self::flags().filter_map(|&(name, value)| self.contains(value).then(|| name)))
.finish()
}
}

fn main() {
// Bitmask that contains Flag1 and Flag3
let bm = Bitmask::Flag1 | Bitmask::Flag3;

println!("{}", bm); // ["Flag1", "Flag3"]
}

0 comments on commit 4d12b19

Please sign in to comment.