-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added custom display example using flags_iter config option
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |