devela 0.27.0

A development layer of coherence.
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// devela::num::fin::bit::ops::wise::impls
//
//! Implements `Bitwise` for the integer primitives.
//

use super::super::_docs::*;
#[allow(unused_imports, reason = "MismatchedCapacity only used for docs")]
use crate::{
    Bitwise,
    MismatchedBounds::{self, IndexOutOfBounds, MismatchedCapacity, MismatchedIndices},
    is,
};

macro_rules! impl_bits_wrapper {
    () => { impl_bits_wrapper![
        u8, u16, u32, u64, u128, usize
    ]; };
    ( $( $t:ty),+ ) => { $( impl_bits_wrapper![@$t]; )+ };

    // `$t`: the primitive type
    (@$t:ty) => {
        /* impl traits */

        #[doc = concat!["# Implementations for `", stringify!($t), "`."]]
        /// ---
        /// TOC
        /// - constants + mask constructors
        /// - single-bit \[get|set\], \[get|set\]_range, \[get|set\]_value_range…
        /// - unset\[_range\]…
        /// - flip, flip_range…
        /// - reverse_range…
        /// - count_ones_range, count_zeros_range…
        /// - find_first_one, find_last_one…
        impl Bitwise::<$t> {
            /* constants */

            /// The size in bits.
            pub const BITS: u32 = <$t>::BITS;

            /* mask constructors */

            #[doc = _DOC_BIT_MASK_RANGE!()]
            #[doc = include_str!("../_benches/mask_range.md")]
            pub const fn mask_range(start: u32, end: u32) -> Self {
                debug_assert![start <= end];
                // a mask with all bits set, from 0 to end:
                let mask_end = is![end == <$t>::BITS -1, !0, (1 << (end + 1)) - 1];
                // a mask with all bits set from 0 to start - 1:
                let mask_start = is![start == 0, 0, (1 << start) - 1];
                Self(mask_end - mask_start)
            }
            #[doc = _DOC_BIT_MASK_RANGE_CHECKED!()]
            #[doc = include_str!("../_benches/mask_checked_range.md")]
            pub const fn mask_checked_range(start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                if start >= <$t>::BITS {
                    Err(IndexOutOfBounds(Some(start as usize)))
                } else if end >= <$t>::BITS {
                    Err(IndexOutOfBounds(Some(end as usize)))
                } else if start > end {
                    Err(MismatchedIndices)
                } else {
                    // create a mask with all bits set, from 0 to end:
                    let mask_end = is![end == <$t>::BITS -1, !0, (1 << (end + 1)) - 1];
                    // create a mask with all bits set from 0 to start - 1:
                    let mask_start = is![start == 0, 0, (1 << start) - 1];
                    Ok(Self(mask_end - mask_start))
                }
            }

            #[must_use]
            #[doc = _DOC_BIT_IS_SET_MASK!()]
            pub const fn is_set_mask(self, mask: $t) -> bool { (self.0 & mask) != 0 }

            #[doc = _DOC_BIT_SET_MASK!()]
            pub const fn set_mask(self, mask: $t) -> Self { Self(self.0 | mask) }

            #[must_use]
            #[doc = _DOC_BIT_IS_UNSET_MASK!()]
            pub const fn is_unset_mask(self, mask: $t) -> bool { (self.0 & mask) == 0 }

            #[doc = _DOC_BIT_UNSET_MASK!()]
            pub const fn unset_mask(self, mask: $t) -> Self { Self(self.0 & !mask) }
        }
        #[doc = concat!["# Get methods for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* get */

            #[doc = _DOC_BIT_GET_RANGE!()]
            pub const fn get_range(self, start: u32, end: u32) -> Self {
                Self(self.0 & Self::mask_range(start, end).0)
            }
            #[doc = _DOC_BIT_GET_CHECKED_RANGE!()]
            pub const fn get_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok(Self(self.0 & mask.0)),
                    Err(e) => Err(e),
                }
            }

            /* get value */

            #[doc = _DOC_BIT_GET_VALUE_RANGE!()]
            pub const fn get_value_range(self, start: u32, end: u32) -> Self {
                Self((self.0 & Self::mask_range(start, end).0) >> start)
            }
            #[doc = _DOC_BIT_GET_VALUE_CHECKED_RANGE!()]
            pub const fn get_value_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok(Self((self.0 & mask.0) >> start)),
                    Err(e) => Err(e),
                }
            }
        }
        #[doc = concat!["# Set ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* set */

            #[doc = _DOC_BIT_IS_SET!()]
            pub const fn is_set(self, nth: u32) -> bool {
                (self.0 & (1 << nth)) != 0
            }
            #[doc = _DOC_BIT_IS_SET_CHECKED!()]
            pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> {
                if nth >= Self::BITS { Err(IndexOutOfBounds(Some(nth as usize))) }
                else { Ok((self.0 & (1 << nth)) != 0) }
            }

            #[doc = _DOC_BIT_SET!()]
            pub const fn set(self, nth: u32) -> Self {
                Self(self.0 | 1 << nth)
            }
            #[doc = _DOC_BIT_SET_CHECKED!()]
            pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                if nth >= Self::BITS { Err(IndexOutOfBounds(Some(nth as usize))) }
                else { Ok(self.set(nth)) }
            }

            #[doc = _DOC_BIT_IS_SET_RANGE!()]
            pub const fn is_set_range(self, start: u32, end: u32) -> bool {
                self.get_range(start, end).0 == Self::mask_range(start, end).0
            }
            #[doc = _DOC_BIT_IS_SET_CHECKED_RANGE!()]
            pub const fn is_set_checked_range(self, start: u32, end: u32,)
                -> Result<bool, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok((self.0 & mask.0) == mask.0),
                    Err(e) => Err(e),
                }
            }

            #[doc = _DOC_BIT_SET_RANGE!()]
            pub const fn set_range(self, start: u32, end: u32) -> Self {
                Self(self.0 | Self::mask_range(start, end).0)
            }
            #[doc = _DOC_BIT_SET_CHECKED_RANGE!()]
            pub const fn set_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok(Self(self.0 | mask.0)),
                    Err(e) => Err(e),
                }
            }

            #[doc = _DOC_BIT_SET_ALL!()]
            pub const fn set_all(self) -> Self { Self(!0) }

            /* set value */

            #[doc = _DOC_BIT_SET_VALUE_RANGE!()]
            pub const fn set_value_range(self, value: $t, start: u32, end: u32) -> Self {
                let mask = Self::mask_range(start, end).0;
                let value_shifted = (value << start) & mask;
                Self((self.0 & !mask) | value_shifted)
            }
            #[doc = _DOC_BIT_SET_VALUE_CHECKED_RANGE!()]
            pub const fn set_value_checked_range(self, value: $t, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let value_shifted = (value << start) & mask.0;
                        Ok(Self((self.0 & !mask.0) | value_shifted))
                    },
                    Err(e) => Err(e),
                }
            }
            #[doc = _DOC_BIT_SET_CHECKED_VALUE_CHECKED_RANGE!()]
            pub const fn set_checked_value_checked_range(self, value: $t, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        if value >= (1 << (end - start)) {
                            let err = crate::MismatchedCapacity
                                ::too_large(value as usize, ( 1 << (end - start)));
                            return Err(MismatchedBounds::from_mismatched_capacity(err));
                        }
                        let value_shifted = (value << start) & mask.0;
                        Ok(Self((self.0 & !mask.0) | value_shifted))
                    },
                    Err(e) => Err(e),
                }
            }
        }
        #[doc = concat!["# Unset ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* unset */

            #[doc = _DOC_BIT_IS_UNSET!()]
            pub const fn is_unset(self, nth: u32) -> bool {
                (self.0 & (1 << nth)) == 0
            }
            #[doc = _DOC_BIT_IS_UNSET_CHECKED!()]
            pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> {
                if nth >= Self::BITS { Err(IndexOutOfBounds(Some(nth as usize))) }
                else { Ok((self.0 & (1 << nth)) == 0) }
            }

            #[doc = _DOC_BIT_UNSET!()]
            pub const fn unset(self, nth: u32) -> Self {
                Self(self.0 & !(1 << nth))
            }
            #[doc = _DOC_BIT_UNSET_CHECKED!()]
            pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                if nth >= Self::BITS { Err(IndexOutOfBounds(Some(nth as usize))) }
                else { Ok(self.unset(nth)) }
            }

            #[doc = _DOC_BIT_IS_UNSET_RANGE!()]
            pub const fn is_unset_range(self, start: u32, end: u32) -> bool {
                self.get_range(start, end).0 == 0
            }
            #[doc = _DOC_BIT_IS_UNSET_CHECKED_RANGE!()]
            pub const fn is_unset_checked_range(self, start: u32, end: u32)
                -> Result<bool, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok((self.0 & mask.0) == 0),
                    Err(e) => Err(e),
                }
            }

            #[doc = _DOC_BIT_UNSET_RANGE!()]
            pub const fn unset_range(self, start: u32, end: u32) -> Self {
                Self(self.0 & !Self::mask_range(start, end).0)
            }
            #[doc = _DOC_BIT_UNSET_CHECKED_RANGE!()]
            pub const fn unset_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok(Self(self.0 & !mask.0)),
                    Err(e) => Err(e),
                }
            }

            #[doc = _DOC_BIT_UNSET_ALL!()]
            pub const fn unset_all(self) -> Self { Self(0) }
        }
        #[doc = concat!["# Flip ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* flip */

            #[doc = _DOC_BIT_FLIP!()]
            pub const fn flip(self, nth: u32) -> Self {
                Self(self.0 ^ (1 << nth))
            }
            #[doc = _DOC_BIT_FLIP_CHECKED!()]
            pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                if nth >= Self::BITS { Err(IndexOutOfBounds(Some(nth as usize))) }
                else { Ok(self.flip(nth)) }
            }

            #[doc = _DOC_BIT_FLIP_RANGE!()]
            pub const fn flip_range(self, start: u32, end: u32) -> Self {
                Self(self.0 ^ Self::mask_range(start, end).0)
            }

            #[doc = _DOC_BIT_FLIP_CHECKED_RANGE!()]
            pub const fn flip_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok(Self(self.0 ^ mask.0)),
                    Err(e) => Err(e),
                }
            }

            #[doc = _DOC_BIT_FLIP_RANGE_IF!()]
            pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self {
                if cond { self.flip_range(start, end) } else { self }
            }
            #[doc = _DOC_BIT_FLIP_CHECKED_RANGE_IF!()]
            pub const fn flip_checked_range_if(self, start: u32, end: u32, cond: bool)
                -> Result<Self, MismatchedBounds> {
                if cond { self.flip_checked_range(start, end) } else { Ok(self) }
            }
        }
        #[doc = concat!["# Reverse ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* reverse */

            #[doc = _DOC_BIT_REVERSE_RANGE!()]
            pub const fn reverse_range(self, start: u32, end: u32) -> Self {
                debug_assert![start <= end];
                // If the entire range of bits is selected, simply reverse all bits
                let range_bits = end - start + 1;
                is![range_bits == Self::BITS, return Self(self.0.reverse_bits())];
                // Create the mask for the range and reverse its bits
                let mask = (((1 as $t) << range_bits) - 1) << start;
                let bits_to_rev = (self.0 & mask) >> start;
                let rev = bits_to_rev.reverse_bits();
                // Shift the reversed bits back to their original position
                let rev_shifted = (rev >> (Self::BITS - range_bits)) << start;
                // Combine with the original number, preserving bits outside the range
                Self((self.0 & !mask) | rev_shifted)
            }

            #[doc = _DOC_BIT_REVERSE_CHECKED_RANGE!()]
            pub const fn reverse_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                if start >= Self::BITS {
                    Err(IndexOutOfBounds(Some(start as usize)))
                } else if end >= <$t>::BITS {
                    Err(IndexOutOfBounds(Some(end as usize)))
                } else if start > end {
                    Err(MismatchedIndices)
                } else {
                    // If the entire range of bits is selected, simply reverse all bits
                    let range_bits = end - start + 1;
                    is![range_bits == Self::BITS, return Ok(Self(self.0.reverse_bits()))];
                    // Create the mask for the range and reverse its bits
                    let mask = (((1 as $t) << range_bits) - 1) << start;
                    let bits_to_rev = (self.0 & mask) >> start;
                    let rev = bits_to_rev.reverse_bits();
                    // Shift the reversed bits back to their original position
                    let rev_shifted = (rev >> (Self::BITS - range_bits)) << start;
                    // Combine with the original number, preserving bits outside the range
                    Ok(Self((self.0 & !mask) | rev_shifted))
                }
            }
        }
        #[doc = concat!["# Count ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* count */

            #[must_use]
            #[doc = _DOC_BIT_COUNT_ONES_RANGE!()]
            pub const fn count_ones_range(self, start: u32, end: u32) -> u32 {
                let masked_bits = self.0 & Self::mask_range(start, end).0;
                masked_bits.count_ones()
            }
            #[doc = _DOC_BIT_COUNT_ONES_RANGE!()]
            pub const fn count_ones_checked_range(self, start: u32, end: u32)
                -> Result<u32, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => Ok((self.0 & mask.0).count_ones()),
                    Err(e) => Err(e),
                }
            }

            #[must_use]
            #[doc = _DOC_BIT_COUNT_ZEROS_RANGE!()]
            pub const fn count_zeros_range(self, start: u32, end: u32) -> u32 {
                let mask = Self::mask_range(start, end).0;
                let masked_bits = self.0 & mask;
                (!masked_bits & mask).count_ones()
            }
            #[doc = _DOC_BIT_COUNT_ZEROS_CHECKED_RANGE!()]
            pub const fn count_zeros_checked_range(self, start: u32, end: u32)
                -> Result<u32, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let masked_bits = self.0 & mask.0;
                        Ok((!masked_bits & mask.0).count_ones())
                    },
                    Err(e) => Err(e),
                }
            }
        }
        #[doc = concat!["# Find ops for `", stringify!($t), "`."]]
        impl Bitwise::<$t> {
            /* find first */

            #[must_use]
            #[doc = _DOC_BIT_FIND_FIRST_ONE_RANGE!()]
            pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> {
                let masked_bits = self.0 & Self::mask_range(start, end).0;
                let mut idx = start;
                while idx <= end {
                    is![(masked_bits & (1 << idx)) != 0, return Some(idx)];
                    idx += 1;
                }
                None
            }
            #[doc = _DOC_BIT_FIND_FIRST_ONE_CHECKED_RANGE!()]
            pub const fn find_first_one_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let masked_bits = self.0 & mask.0;
                        let mut idx = start;
                        while idx <= end {
                            is![(masked_bits & (1 << idx)) != 0, return Ok(Some(idx))];
                            idx += 1;
                        }
                        Ok(None)
                    },
                    Err(e) => Err(e),
                }
            }

            #[must_use]
            #[doc = _DOC_BIT_FIND_FIRST_ZERO_RANGE!()]
            pub const fn find_first_zero_range(self, start: u32, end: u32)
                -> Option<u32> {
                let masked_bits = !(self.0 & Self::mask_range(start, end).0);
                let mut idx = start;
                while idx <= end {
                    is![(masked_bits & (1 << idx)) != 0, return Some(idx)];
                    idx += 1;
                }
                None
            }
            #[doc = _DOC_BIT_FIND_FIRST_ZERO_CHECKED_RANGE!()]
            pub const fn find_first_zero_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let masked_bits = !(self.0 & mask.0);
                        let mut idx = start;
                        while idx <= end {
                            is![(masked_bits & (1 << idx)) != 0, return Ok(Some(idx))];
                            idx += 1;
                        }
                        Ok(None)
                    },
                    Err(e) => Err(e),
                }
            }

            /* find last */

            #[must_use]
            #[doc = _DOC_BIT_FIND_LAST_ONE_RANGE!()]
            pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> {
                let masked_bits = self.0 & Self::mask_range(start, end).0;
                let mut idx = end;
                loop {
                    is![(masked_bits & (1 << idx)) != 0, return Some(idx)];
                    is![idx == start, break];
                    idx -= 1;
                }
                None
            }

            #[doc = _DOC_BIT_FIND_LAST_ONE_CHECKED_RANGE!()]
            pub const fn find_last_one_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let masked_bits = self.0 & mask.0;
                        let mut idx = end;
                        loop {
                            is![(masked_bits & (1 << idx)) != 0, return Ok(Some(idx))];
                            is![idx == start, break];
                            idx -= 1;
                        }
                        Ok(None)
                    },
                    Err(e) => Err(e),
                }
            }

            #[must_use]
            #[doc = _DOC_BIT_FIND_LAST_ZERO_RANGE!()]
            pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> {
                let masked_bits = !(self.0 & Self::mask_range(start, end).0);
                let mut idx = end;
                loop {
                    is![(masked_bits & (1 << idx)) != 0, return Some(idx)];
                    is![idx == start, break];
                    idx -= 1;
                }
                None
            }
            #[doc = _DOC_BIT_FIND_LAST_ZERO_CHECKED_RANGE!()]
            pub const fn find_last_zero_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                match Self::mask_checked_range(start, end) {
                    Ok(mask) => {
                        let masked_bits = !(self.0 & mask.0);
                        let mut idx = end;
                        loop {
                            is![(masked_bits & (1 << idx)) != 0, return Ok(Some(idx))];
                            is![idx == start, break];
                            idx -= 1;
                        }
                        Ok(None)
                    },
                    Err(e) => Err(e),
                }
            }
        }
    };
}
impl_bits_wrapper![];