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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use core::ops::{BitAnd, BitOr, BitXor, Not};
use crate::{BitFlag, BitFlags, BitFlagsExt, BitFlagsSpec, util};
/// Extension trait providing generic, non-const equivalents of the
/// type-specific inherent methods on [`BitFlag`].
///
/// The inherent methods are implemented for each concrete integer type so they
/// can be `const`.
/// This trait exposes the same operations for generic `T`.
pub trait BitFlagExt<T, U>: Sized
where
U: ?Sized,
{
/// Creates a bit flag from a [`BitFlags`] set.
///
/// Returns `Some` if `flags` contains exactly one set bit and that bit is
/// valid according to [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// Returns `None` if `flags` is empty, has more than one bit set, or the
/// single set bit is not valid.
///
/// This is equivalent to [`BitFlag::try_from_bits`] on `flags.bits()`.
#[must_use]
fn try_from_flags(flags: BitFlags<T, U>) -> Option<Self>
where
T: 'static + Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a validated bit flag for bit index `i`.
///
/// Returns `None` if `i` is not set in
/// [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// The index `i` must be less than the number of bits in the
/// storage type.
#[must_use]
fn try_from_bit(i: u8) -> Option<Self>
where
T: 'static + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a validated bit flag from `bits` if exactly one bit is
/// set and that bit is valid according to
/// [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// Returns `None` if `bits` is zero, has more than one bit set, or
/// the single set bit is not valid.
#[must_use]
fn try_from_bits(bits: T) -> Option<Self>
where
T: 'static + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a single-bit flag from `bits` if exactly one bit is set.
///
/// The single set bit is not checked against
/// [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// Returns `None` if `bits` is `0` or has more than one bit set.
#[must_use]
fn try_from_bits_retain(bits: T) -> Option<Self>
where
T: util::IntOpsExt;
/// Returns `true` if this flag refers to a valid bit.
///
/// A flag is valid when its bit index is within the width of `T` and the
/// corresponding bit is present in [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// [`BitFlag::from_bit_retain`] does not validate the bit index, so a
/// `BitFlag` constructed through it may be invalid.
#[must_use]
fn is_valid(&self) -> bool
where
T: 'static + Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Returns the single-bit mask for this flag, or `None` if the bit index is
/// outside the width of `T`.
///
/// The mask is `1 << bit`. It is not checked against
/// [`BitFlagsSpec::VALID_BITS_MASK`]; use [`BitFlag::is_valid`] when tag
/// validity is required.
#[must_use]
fn mask(&self) -> Option<T>
where
T: util::IntOpsExt;
/// Returns the single-bit mask for this flag, or `0` if the bit index is
/// outside the width of `$ty`.
///
/// This is the const-friendly equivalent of `self.mask().unwrap_or(0)`.
#[must_use]
fn mask_or_default(&self) -> T
where
T: Default + util::IntOpsExt;
/// Returns the intersection of `self` and `other`.
///
/// This is a raw-bit operation.
/// Invalid bits in either operand are preserved when they are set
/// in both operands.
#[must_use]
fn intersection(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitAnd<T, Output = T> + util::IntOpsExt;
/// Returns the union of `self` and `other`.
///
/// This is a raw-bit operation.
/// Invalid bits in either operand are preserved.
#[must_use]
fn union(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitOr<T, Output = T> + util::IntOpsExt;
/// Returns the difference of `self` and `other`.
///
/// The result contains bits that are set in `self` but not in
/// `other`.
/// This is a raw-bit operation.
/// Invalid bits already present in `self` are preserved unless they
/// are also set in `other`.
#[must_use]
fn difference(self, other: Self) -> BitFlags<T, U>
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt;
/// Returns the symmetric difference of `self` and `other`.
///
/// The result contains bits that are set in exactly one of `self`
/// or `other`.
/// This is a raw-bit operation.
/// Invalid bits participate and may be preserved.
#[must_use]
fn symmetric_difference(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitXor<T, Output = T> + util::IntOpsExt;
/// Returns the complement of `self`.
///
/// All valid bits not set in `self` become set, and all invalid
/// bits become unset.
/// The result always contains only valid bits.
#[must_use]
fn complement(self) -> BitFlags<T, U>
where
T: 'static + Copy + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
}
impl<T, U> BitFlagExt<T, U> for BitFlag<T, U>
where
U: ?Sized,
{
#[inline]
fn try_from_flags(flags: BitFlags<T, U>) -> Option<Self>
where
T: 'static + Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
Self::try_from_bits(flags.bits())
}
#[inline]
fn try_from_bit(i: u8) -> Option<Self>
where
T: 'static + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
let bits = T::ONE.checked_shl(u32::from(i))?;
((i < T::BITS) && (bits & !U::VALID_BITS_MASK) == T::ZERO).then(|| Self::from_bit_retain(i))
}
#[inline]
fn try_from_bits(bits: T) -> Option<Self>
where
T: 'static + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
if bits.count_ones() == 1
&& let Some(bit) = bits.lowest_one()
&& let Ok(bit) = u8::try_from(bit)
{
Self::try_from_bit(bit)
} else {
None
}
}
#[inline]
fn try_from_bits_retain(bits: T) -> Option<Self>
where
T: util::IntOpsExt,
{
if bits.count_ones() == 1
&& let Some(bit) = bits.lowest_one()
&& let Ok(bit) = u8::try_from(bit)
{
Some(Self::from_bit_retain(bit))
} else {
None
}
}
#[inline]
fn is_valid(&self) -> bool
where
T: 'static + Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
(self.bit() < T::BITS) && BitFlags::<T, U>::from_flag(*self).is_valid()
}
#[inline]
fn mask(&self) -> Option<T>
where
T: util::IntOpsExt,
{
T::ONE.checked_shl(u32::from(self.bit()))
}
#[inline]
fn mask_or_default(&self) -> T
where
T: Default + util::IntOpsExt,
{
self.mask().unwrap_or_default()
}
#[inline]
fn intersection(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitAnd<T, Output = T> + util::IntOpsExt,
{
<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(self)
.intersection(<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(other))
}
#[inline]
fn union(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitOr<T, Output = T> + util::IntOpsExt,
{
<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(self)
.union(<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(other))
}
#[inline]
fn difference(self, other: Self) -> BitFlags<T, U>
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
{
<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(self)
.difference(<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(other))
}
#[inline]
fn symmetric_difference(self, other: Self) -> BitFlags<T, U>
where
T: Copy + BitXor<T, Output = T> + util::IntOpsExt,
{
<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(self)
.symmetric_difference(<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(other))
}
#[inline]
fn complement(self) -> BitFlags<T, U>
where
T: 'static + Copy + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
<BitFlags<T, U> as BitFlagsExt<T, U>>::from_flag(self).complement()
}
}