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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use core::fmt;
/// Frame flags.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Flags(u8);
impl fmt::Debug for Flags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().finish()
}
}
impl Flags {
/// Create a new set of frame flags with no flag set.
#[inline]
#[must_use]
pub const fn empty() -> Self {
Self(0)
}
/// Create a new set of frame flags from `bits`.
///
/// The upper 6 bits of `bits` are discarded.
#[inline]
#[must_use]
pub const fn from_bits(bits: u8) -> Self {
Self(bits & 0b11)
}
/// Get the bit representation of the flags.
#[inline]
#[must_use]
pub const fn bits(self) -> u8 {
self.0
}
/// Check whether any flag in `other` is set in `self`.
///
/// If `other` contains only one flag, this method behaves identical to [`is_all_set()`].
///
/// [`is_all_set()`]: Self::is_all_set
#[inline]
#[must_use]
pub const fn is_any_set(self, other: Self) -> bool {
self.0 & other.0 != 0
}
/// Check whether all flags in `other` are set in `self`.
///
/// If `other` contains only one flag, this method behaves identical to [`is_any_set()`].
///
/// [`is_any_set()`]: Self::is_any_set
#[inline]
#[must_use]
pub const fn is_all_set(self, other: Self) -> bool {
(self.0 & other.0) ^ other.0 == 0
}
/// Set the flags of `other` in `self`.
#[inline]
pub fn set(&mut self, other: Self) {
self.0 |= other.0;
}
/// Set the flags of `other` in `self` if the predicate _f_ returns `true`.
#[inline]
pub fn set_if<F>(&mut self, other: Self, f: F)
where
F: FnOnce() -> bool,
{
if f() {
self.set(other);
}
}
/// Unset the flags of `other` in `self`.
#[inline]
pub fn unset(&mut self, other: Self) {
self.0 &= !other.0;
}
/// Unset the flags of `other` in `self` if the predicate _f_ returns `true`.
#[inline]
pub fn unset_if<F>(&mut self, other: Self, f: F)
where
F: FnOnce() -> bool,
{
if f() {
self.unset(other);
}
}
/// Set or unset the flags of `other` in `self` depending on `set`.
///
/// If `set` is `true`, this method will set the flags of `other` in `self`. If `set`
/// is `false`, it will unset them.
#[inline]
pub fn set_or_unset(&mut self, other: Self, set: bool) {
if set {
self.set(other);
} else {
self.unset(other);
}
}
/// Set or unset the flags of `other` in `self` based on a predicate _f_.
#[inline]
pub fn set_or_unset_with<F>(&mut self, other: Self, f: F)
where
F: FnOnce() -> bool,
{
self.set_or_unset(other, f());
}
}