-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_data_structures.rs
58 lines (55 loc) · 1.4 KB
/
custom_data_structures.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use bitflags::bitflags;
use std::fmt::{Display, Formatter, Result};
bitflags! {
/// MyFlags is a set of flags.
///
/// # Example
///
/// ```
/// use data_structures::MyFlags;
///
/// let e1 = MyFlags::FLAG_A | MyFlags::FLAG_C;
/// println!("{:?}", e1); // MyFlags(FLAG_A | FLAG_C)
/// ```
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct MyFlags: u32 {
const FLAG_A = 0b00000001;
const FLAG_B = 0b00000010;
const FLAG_C = 0b00000100;
const FLAG_ABC = Self::FLAG_A.bits()
| Self::FLAG_B.bits()
| Self::FLAG_C.bits();
}
}
impl MyFlags {
/// Clears all flags.
///
/// # Example
///
/// ```
/// use data_structures::MyFlags;
///
/// let mut flags = MyFlags::FLAG_ABC;
/// flags.clear();
/// println!("{:?}", flags); // MyFlags(0)
/// ```
pub fn clear(&mut self) -> &mut MyFlags {
*self = MyFlags::empty();
self
}
}
impl Display for MyFlags {
/// Formats the flag as a 32-bit binary number.
///
/// # Example
///
/// ```
/// use data_structures::MyFlags;
///
/// let flags = MyFlags::FLAG_ABC;
/// println!("{}", flags); // 00000000000000000000000000000111
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{:032b}", self.bits())
}
}