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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
use crate::{BitFlag, BitFlags, BitFlagsSpec, util};
/// Extension trait providing generic, non-const equivalents of the
/// type-specific inherent methods on [`BitFlags`].
///
/// 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 BitFlagsExt<T, U>: Sized
where
U: ?Sized,
{
/// Creates a flag set from a single [`BitFlag`].
///
/// The bit index is not validated against
/// [`BitFlagsSpec::VALID_BITS_MASK`].
/// The resulting value may contain an invalid bit.
///
/// If the flag's index is outside the width of `T`, the result is
/// [`BitFlags::empty`].
#[must_use]
fn from_flag(bit_flag: BitFlag<T, U>) -> Self
where
T: util::IntOpsExt;
/// Creates a flag set with only the bit at index `bit` set.
///
/// The bit index is not validated against
/// [`BitFlagsSpec::VALID_BITS_MASK`].
/// The resulting value may contain an invalid bit. Use
/// [`BitFlags::try_from_bit`] or [`BitFlags::from_bits_truncate`] when
/// validation or truncation is required.
///
/// If `bit` is outside the width of `T`, the result is
/// [`BitFlags::empty`].
#[must_use]
fn from_bit_retain(bit: u8) -> Self
where
T: util::IntOpsExt;
/// Creates a flag set with only the bit at index `bit` set, clearing the
/// bit if it is not valid for the tag.
///
/// This is the bit-index counterpart to [`BitFlags::from_bits_truncate`].
/// If `bit` is outside the width of `T`, or if the corresponding bit is
/// not present in [`BitFlagsSpec::VALID_BITS_MASK`], the result is
/// [`BitFlags::empty`].
///
/// The result always contains either exactly one bit or no bits.
///
/// Use [`BitFlags::try_from_bit`] when the caller needs to distinguish a
/// valid index from an invalid one.
#[must_use]
fn from_bit_truncate(bit: u8) -> Self
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a flag set from bit index `bit` if that bit is valid.
///
/// Returns `None` if `bit` is outside the width of `T` or if the resulting
/// bit is not present in [`BitFlagsSpec::VALID_BITS_MASK`].
#[must_use]
fn try_from_bit(bit: u8) -> Option<Self>
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a flag set from raw bits if all set bits are valid.
///
/// Returns `None` if `bits` contains any bit not present in
/// [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// Use [`BitFlags::from_bits_retain`] to keep invalid bits, or
/// [`BitFlags::from_bits_truncate`] to clear them.
#[doc(alias = "from_bits")]
#[must_use]
fn try_from_bits(bits: T) -> Option<Self>
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Creates a flag set from raw bits, clearing any bits not present in
/// [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// This always succeeds and never stores invalid bits.
#[must_use]
fn from_bits_truncate(bits: T) -> Self
where
T: BitAnd<T, Output = T>,
U: BitFlagsSpec<T>;
/// Returns `true` if no bits are set.
///
/// This checks the raw stored bits.
/// A value containing only invalid bits is not empty.
#[must_use]
fn is_empty(&self) -> bool
where
T: Copy + PartialEq + util::IntOpsExt;
/// Returns `true` if all set bits in `self` are valid for this tag.
///
/// A `BitFlags` value can contain bits not present in
/// [`BitFlagsSpec::VALID_BITS_MASK`] after construction with
/// [`BitFlags::from_bits_retain`] or after raw-bit operations such as
/// [`BitFlags::insert`], [`BitFlags::toggle`], and [`BitFlags::union`].
/// This method returns `false` for such values.
///
/// Use [`BitFlags::truncate`] or [`BitFlags::from_bits_truncate`] to
/// restrict a value to its valid bits.
#[doc(alias = "is_all")]
#[must_use]
fn is_valid(&self) -> bool
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>;
/// Returns `true` if `self` and `other` have at least one set bit in
/// common.
///
/// This compares raw bits.
/// Invalid bits participate in the comparison if they are present.
#[must_use]
fn intersects(&self, other: Self) -> bool
where
T: Copy + PartialEq + BitAnd<T, Output = T> + util::IntOpsExt;
/// Returns `true` if all bits set in `other` are also set in `self`.
///
/// This compares raw bits.
/// Invalid bits participate in the comparison if they are present.
#[must_use]
fn contains(&self, other: Self) -> bool
where
T: Copy + PartialEq + BitAnd<T, Output = T>;
/// Removes all bits from `self`, including any invalid bits.
///
/// This is equivalent to assigning [`BitFlags::empty`] to `self`.
fn clear(&mut self)
where
T: util::IntOpsExt;
/// Clears all bits not present in [`BitFlagsSpec::VALID_BITS_MASK`].
///
/// This leaves `self` unchanged if it already contains only valid bits.
fn truncate(&mut self)
where
T: BitAndAssign<T>,
U: BitFlagsSpec<T>;
/// Inserts all bits from `other`.
///
/// This is a raw-bit operation.
/// It does not validate or clear invalid bits.
/// Inserting `other` can introduce invalid bits into `self`.
fn insert(&mut self, other: Self)
where
T: Copy + BitOrAssign<T>;
/// Removes all bits from `other`.
///
/// This is a raw-bit operation.
/// It cannot introduce new bits, but it does not clear any other
/// invalid bits that may already be present.
fn remove(&mut self, other: Self)
where
T: Copy + Not<Output = T> + BitAndAssign<T>;
/// Toggles all bits from `other`.
///
/// This is a raw-bit operation.
/// It does not validate or clear invalid bits.
/// Toggling `other` can introduce invalid bits into `self`.
fn toggle(&mut self, other: Self)
where
T: Copy + BitXorAssign<T>;
/// Inserts `other` if `value` is `true`, or removes `other` if
/// `value` is `false`.
///
/// This is a raw-bit operation and follows the same invalid-bit
/// behavior as [`BitFlags::insert`] and [`BitFlags::remove`].
fn set(&mut self, other: Self, value: bool)
where
T: Copy + Not<Output = T> + BitAndAssign<T> + BitOrAssign<T>;
/// 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) -> Self
where
T: Copy + BitAnd<T, Output = T>;
/// 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) -> Self
where
T: Copy + BitOr<T, Output = T>;
/// 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) -> Self
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T>;
/// 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) -> Self
where
T: Copy + BitXor<T, Output = T>;
/// 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) -> Self
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T>,
U: BitFlagsSpec<T>;
}
impl<T, U> BitFlagsExt<T, U> for BitFlags<T, U>
where
U: ?Sized,
{
#[inline]
fn from_flag(bit_flag: BitFlag<T, U>) -> Self
where
T: util::IntOpsExt,
{
Self::from_bit_retain(bit_flag.bit())
}
#[inline]
fn from_bit_retain(bit: u8) -> Self
where
T: util::IntOpsExt,
{
Self::from(BitFlag::from_bit_retain(bit))
}
#[inline]
fn from_bit_truncate(bit: u8) -> Self
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
Self::try_from_bit(bit).unwrap_or(Self::empty())
}
#[inline]
fn try_from_bit(bit: u8) -> Option<Self>
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
T::ONE
.checked_shl(u32::from(bit))
.and_then(Self::try_from_bits)
}
#[inline]
fn try_from_bits(bits: T) -> Option<Self>
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
(bits & !U::VALID_BITS_MASK == T::ZERO).then(|| Self::from_bits_retain(bits))
}
#[inline]
fn from_bits_truncate(bits: T) -> Self
where
T: BitAnd<T, Output = T>,
U: BitFlagsSpec<T>,
{
Self::from_bits_retain(bits & U::VALID_BITS_MASK)
}
#[inline]
fn is_empty(&self) -> bool
where
T: Copy + PartialEq + util::IntOpsExt,
{
self.bits() == T::ZERO
}
#[inline]
fn is_valid(&self) -> bool
where
T: Copy + PartialEq + Not<Output = T> + BitAnd<T, Output = T> + util::IntOpsExt,
U: BitFlagsSpec<T>,
{
self.bits() & !U::VALID_BITS_MASK == T::ZERO
}
#[inline]
fn intersects(&self, other: Self) -> bool
where
T: Copy + PartialEq + BitAnd<T, Output = T> + util::IntOpsExt,
{
(self.bits() & other.bits()) != T::ZERO
}
#[inline]
fn contains(&self, other: Self) -> bool
where
T: Copy + PartialEq + BitAnd<T, Output = T>,
{
(self.bits() & other.bits()) == other.bits()
}
#[inline]
fn clear(&mut self)
where
T: util::IntOpsExt,
{
*self = Self::empty();
}
#[inline]
fn truncate(&mut self)
where
T: BitAndAssign<T>,
U: BitFlagsSpec<T>,
{
*self.bits_mut() &= U::VALID_BITS_MASK;
}
#[inline]
fn insert(&mut self, other: Self)
where
T: Copy + BitOrAssign<T>,
{
*self.bits_mut() |= other.bits();
}
#[inline]
fn remove(&mut self, other: Self)
where
T: Copy + Not<Output = T> + BitAndAssign<T>,
{
*self.bits_mut() &= !other.bits();
}
#[inline]
fn toggle(&mut self, other: Self)
where
T: Copy + BitXorAssign<T>,
{
*self.bits_mut() ^= other.bits();
}
#[inline]
fn set(&mut self, other: Self, value: bool)
where
T: Copy + Not<Output = T> + BitAndAssign<T> + BitOrAssign<T>,
{
if value {
self.insert(other);
} else {
self.remove(other);
}
}
#[inline]
fn intersection(self, other: Self) -> Self
where
T: Copy + BitAnd<T, Output = T>,
{
Self::from_bits_retain(self.bits() & other.bits())
}
#[inline]
fn union(self, other: Self) -> Self
where
T: Copy + BitOr<T, Output = T>,
{
Self::from_bits_retain(self.bits() | other.bits())
}
#[inline]
fn difference(self, other: Self) -> Self
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T>,
{
Self::from_bits_retain(self.bits() & !other.bits())
}
#[inline]
fn symmetric_difference(self, other: Self) -> Self
where
T: Copy + BitXor<T, Output = T>,
{
Self::from_bits_retain(self.bits() ^ other.bits())
}
#[inline]
fn complement(self) -> Self
where
T: Copy + Not<Output = T> + BitAnd<T, Output = T>,
U: BitFlagsSpec<T>,
{
Self::from_bits_retain(!self.bits() & U::VALID_BITS_MASK)
}
}