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
// devela::num::fin::bit::ops
//
//! Defines the [`BitOps`] trait.
//
// TOC
// - definition
// - impls

use super::_docs::*;
#[cfg(doc)]
use crate::MismatchedBounds::{IndexOutOfBounds, MismatchedCapacity, MismatchedIndices};
use crate::{Bitwise, MismatchedBounds};

#[doc = crate::_tags!(num namespace)]
/// Provides bitwise operations on `T`.
#[doc = crate::_doc_location!("num/fin")]
///
/// See also [`Bitwise`] for the related const wrapper.
#[rustfmt::skip]
pub trait BitOps where Self: Sized {
    /// The inner type for the bit representation.
    type Inner;

    // const BITS: u32; // no need because the primitives already have this.

    /* mask */

    #[must_use]
    #[doc = _DOC_BIT_MASK_RANGE!()]
    #[doc = include_str!("_benches/mask_range.md")]
    fn bit_mask_range(start: u32, end: u32) -> Self;

    #[doc = _DOC_BIT_MASK_RANGE_CHECKED!()]
    #[doc = include_str!("_benches/mask_checked_range.md")]
    fn bit_mask_checked_range(start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_IS_SET_MASK!()]
    fn bit_is_set_mask(self, mask: Self::Inner) -> bool;

    #[doc = _DOC_BIT_SET_MASK!()]
    fn bit_set_mask(self, mask: Self::Inner) -> Self;

    #[must_use]
    #[doc = _DOC_BIT_IS_UNSET_MASK!()]
    fn bit_is_unset_mask(self, mask: Self::Inner) -> bool;

    #[doc = _DOC_BIT_UNSET_MASK!()]
    fn bit_unset_mask(self, mask: Self::Inner) -> Self;

    /* get */

    #[must_use]
    #[doc = _DOC_BIT_GET_RANGE!()]
    fn bit_get_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_GET_CHECKED_RANGE!()]
    fn bit_get_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    /* get value */

    #[must_use]
    #[doc = _DOC_BIT_GET_VALUE_RANGE!()]
    fn bit_get_value_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_GET_VALUE_CHECKED_RANGE!()]
    fn bit_get_value_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    /* set */

    #[must_use]
    #[doc = _DOC_BIT_IS_SET!()]
    fn bit_is_set(self, nth: u32) -> bool;
    #[doc = _DOC_BIT_IS_SET_CHECKED!()]
    fn bit_is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_SET!()]
    fn bit_set(self, nth: u32) -> Self;
    #[doc = _DOC_BIT_SET_CHECKED!()]
    fn bit_set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_SET_RANGE!()]
    fn bit_is_set_range(self, start: u32, end: u32) -> bool;
    #[doc = _DOC_BIT_SET_CHECKED_RANGE!()]
    fn bit_is_set_checked_range(self, start: u32, end: u32) -> Result<bool, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_SET_RANGE!()]
    fn bit_set_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_SET_CHECKED_RANGE!()]
    fn bit_set_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    #[doc = _DOC_BIT_SET_ALL!()]
    fn bit_set_all(self) -> Self;

    /* set value */

    #[must_use]
    #[doc = _DOC_BIT_SET_VALUE_RANGE!()]
    fn bit_set_value_range(self, value: Self::Inner, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_SET_VALUE_CHECKED_RANGE!()]
    fn bit_set_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
        -> Result<Self, MismatchedBounds>;
    #[doc = _DOC_BIT_SET_CHECKED_VALUE_CHECKED_RANGE!()]
    fn bit_set_checked_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
        -> Result<Self, MismatchedBounds>;

    /* unset */

    #[must_use]
    #[doc = _DOC_BIT_IS_UNSET!()]
    fn bit_is_unset(self, nth: u32) -> bool;
    #[doc = _DOC_BIT_IS_UNSET_CHECKED!()]
    fn bit_is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_UNSET!()]
    fn bit_unset(self, nth: u32) -> Self;
    #[doc = _DOC_BIT_UNSET_CHECKED!()]
    fn bit_unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_UNSET_RANGE!()]
    fn bit_is_unset_range(self, start: u32, end: u32) -> bool;
    #[doc = _DOC_BIT_UNSET_CHECKED_RANGE!()]
    fn bit_is_unset_checked_range(self, start: u32, end: u32) -> Result<bool, MismatchedBounds>;


    #[must_use]
    #[doc = _DOC_BIT_UNSET_RANGE!()]
    fn bit_unset_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_UNSET_CHECKED_RANGE!()]
    fn bit_unset_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    #[doc = _DOC_BIT_UNSET_ALL!()]
    fn bit_unset_all(self) -> Self;

    /* flip */

    #[must_use]
    #[doc = _DOC_BIT_FLIP!()]
    fn bit_flip(self, nth: u32) -> Self;
    #[doc = _DOC_BIT_FLIP_CHECKED!()]
    fn bit_flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_FLIP_RANGE!()]
    fn bit_flip_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_FLIP_CHECKED_RANGE!()]
    fn bit_flip_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_FLIP_RANGE_IF!()]
    fn bit_flip_range_if(self, start: u32, end: u32, cond: bool) -> Self;
    #[doc = _DOC_BIT_FLIP_CHECKED_RANGE_IF!()]
    fn bit_flip_checked_range_if(self, start: u32, end: u32, cond: bool)
        -> Result<Self, MismatchedBounds>;

    /* reverse */

    #[must_use]
    #[doc = _DOC_BIT_REVERSE_RANGE!()]
    fn bit_reverse_range(self, start: u32, end: u32) -> Self;
    #[doc = _DOC_BIT_REVERSE_CHECKED_RANGE!()]
    fn bit_reverse_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;

    /* count */

    #[must_use]
    #[doc = _DOC_BIT_COUNT_ONES_RANGE!()]
    fn bit_count_ones_range(self, start: u32, end: u32) -> u32;
    #[doc = _DOC_BIT_COUNT_ONES_CHECKED_RANGE!()]
    fn bit_count_ones_checked_range(self, start: u32, end: u32) -> Result<u32, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_COUNT_ZEROS_RANGE!()]
    fn bit_count_zeros_range(self, start: u32, end: u32) -> u32;
    #[doc = _DOC_BIT_COUNT_ZEROS_CHECKED_RANGE!()]
    fn bit_count_zeros_checked_range(self, start: u32, end: u32) -> Result<u32, MismatchedBounds>;

    /* find first */

    #[must_use]
    #[doc = _DOC_BIT_FIND_FIRST_ONE_RANGE!()]
    fn bit_find_first_one_range(self, start: u32, end: u32) -> Option<u32>;
    #[doc = _DOC_BIT_FIND_FIRST_ONE_CHECKED_RANGE!()]
    fn bit_find_first_one_checked_range(self, start: u32, end: u32)
        -> Result<Option<u32>, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_FIND_FIRST_ZERO_RANGE!()]
    fn bit_find_first_zero_range(self, start: u32, end: u32) -> Option<u32>;
    #[doc = _DOC_BIT_FIND_FIRST_ZERO_CHECKED_RANGE!()]
    fn bit_find_first_zero_checked_range(self, start: u32, end: u32)
        -> Result<Option<u32>, MismatchedBounds>;

    /* find last */

    #[must_use]
    #[doc = _DOC_BIT_FIND_LAST_ONE_RANGE!()]
    fn bit_find_last_one_range(self, start: u32, end: u32) -> Option<u32>;
    #[doc = _DOC_BIT_FIND_LAST_ONE_CHECKED_RANGE!()]
    fn bit_find_last_one_checked_range(self, start: u32, end: u32)
        -> Result<Option<u32>, MismatchedBounds>;

    #[must_use]
    #[doc = _DOC_BIT_FIND_LAST_ZERO_RANGE!()]
    fn bit_find_last_zero_range(self, start: u32, end: u32) -> Option<u32>;
    #[doc = _DOC_BIT_FIND_LAST_ZERO_CHECKED_RANGE!()]
    fn bit_find_last_zero_checked_range(self, start: u32, end: u32)
        -> Result<Option<u32>, MismatchedBounds>;
}

macro_rules! impl_bit_ops {
    () => {
        impl_bit_ops![
            u8, u16, u32, u64, u128, usize
        ];
    };

    // `$t`: the type to implement the trait for.
    ($($t:ty),+) => { $( impl_bit_ops![@$t]; )+ };
    (@$t:ty) => {
        impl BitOps for $t {
            type Inner = $t;

            /* mask */

            fn bit_mask_range(start: u32, end: u32) -> Self {
                Bitwise::<$t>::mask_range(start, end).0
            }
            fn bit_mask_checked_range(start: u32, end: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise::<$t>::mask_checked_range(start, end)?.0)
            }

            #[doc = _DOC_BIT_IS_SET_MASK!()]
            fn bit_is_set_mask(self, mask: $t) -> bool { Bitwise(self).is_set_mask(mask) }

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

            #[doc = _DOC_BIT_IS_UNSET_MASK!()]
            fn bit_is_unset_mask(self, mask: $t) -> bool { Bitwise(self).is_unset_mask(mask) }

            #[doc = _DOC_BIT_UNSET_MASK!()]
            fn bit_unset_mask(self, mask: $t) -> Self { Bitwise(self).unset_mask(mask).0 }

            /* get */

            fn bit_get_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).get_range(start, end).0
            }
            fn bit_get_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).get_checked_range(start, end)?.0)
            }

            /* get value */

            fn bit_get_value_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).get_value_range(start, end).0
            }
            fn bit_get_value_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).get_value_checked_range(start, end)?.0)
            }

            /* set */

            fn bit_is_set(self, nth: u32) -> bool { Bitwise(self).is_set(nth) }
            fn bit_is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> {
                Bitwise(self).is_set_checked(nth)
            }
            fn bit_set(self, nth: u32) -> Self { Bitwise(self).set(nth).0 }
            fn bit_set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).set_checked(nth)?.0)
            }

            fn bit_is_set_range(self, start: u32, end: u32) -> bool {
                Bitwise(self).is_set_range(start, end)
            }
            fn bit_is_set_checked_range(self, start: u32, end: u32)
                -> Result<bool, MismatchedBounds> {
                Bitwise(self).is_set_checked_range(start, end)
            }

            fn bit_set_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).set_range(start, end).0
            }
            fn bit_set_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).set_checked_range(start, end)?.0)
            }

            fn bit_set_all(self) -> Self { Bitwise(self).set_all().0 }

            /* set value */

            fn bit_set_value_range(self, value: Self::Inner, start: u32, end: u32) -> Self {
                Bitwise(self).set_value_range(value, start, end).0
            }
            fn bit_set_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).set_value_checked_range(value, start, end)?.0)
            }
            fn bit_set_checked_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).set_checked_value_checked_range(value, start, end)?.0)
            }

            /* unset */

            fn bit_is_unset(self, nth: u32) -> bool { Bitwise(self).is_unset(nth) }
            fn bit_is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> {
                Bitwise(self).is_unset_checked(nth)
            }
            fn bit_unset(self, nth: u32) -> Self { Bitwise(self).unset(nth).0 }
            fn bit_unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).unset_checked(nth)?.0)
            }

            fn bit_is_unset_range(self, start: u32, end: u32) -> bool {
                Bitwise(self).is_unset_range(start, end)
            }
            fn bit_is_unset_checked_range(self, start: u32, end: u32)
                -> Result<bool, MismatchedBounds> {
                Bitwise(self).is_unset_checked_range(start, end)
            }

            fn bit_unset_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).unset_range(start, end).0
            }
            fn bit_unset_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).unset_checked_range(start, end)?.0)
            }

            fn bit_unset_all(self) -> Self { Bitwise(self).unset_all().0 }

            /* flip */

            fn bit_flip(self, nth: u32) -> Self { Bitwise(self).flip(nth).0 }
            fn bit_flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).flip_checked(nth)?.0)
            }

            fn bit_flip_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).flip_range(start, end).0
            }
            fn bit_flip_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).flip_checked_range(start, end)?.0)
            }

            fn bit_flip_range_if(self, start: u32, end: u32, cond: bool) -> Self {
                Bitwise(self).flip_range_if(start, end, cond).0
            }
            fn bit_flip_checked_range_if(self, start: u32, end: u32, cond: bool)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).flip_checked_range_if(start, end, cond)?.0)
            }

            /* reverse */

            fn bit_reverse_range(self, start: u32, end: u32) -> Self {
                Bitwise(self).reverse_range(start, end).0
            }
            fn bit_reverse_checked_range(self, start: u32, end: u32)
                -> Result<Self, MismatchedBounds> {
                Ok(Bitwise(self).reverse_checked_range(start, end)?.0)
            }

            /* count */

            fn bit_count_ones_range(self, start: u32, end: u32) -> u32 {
                Bitwise(self).count_ones_range(start, end)
            }
            fn bit_count_ones_checked_range(self, start: u32, end: u32)
                -> Result<u32, MismatchedBounds> {
                Bitwise(self).count_ones_checked_range(start, end)
            }
            fn bit_count_zeros_range(self, start: u32, end: u32) -> u32 {
                Bitwise(self).count_zeros_range(start, end)
            }
            fn bit_count_zeros_checked_range(self, start: u32, end: u32)
                -> Result<u32, MismatchedBounds> {
                Bitwise(self).count_zeros_checked_range(start, end)
            }

            /* find first */

            fn bit_find_first_one_range(self, start: u32, end: u32) -> Option<u32> {
                Bitwise(self).find_first_one_range(start, end)
            }
            fn bit_find_first_one_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                Bitwise(self).find_first_one_checked_range(start, end)
            }
            fn bit_find_first_zero_range(self, start: u32, end: u32) -> Option<u32> {
                Bitwise(self).find_first_zero_range(start, end)
            }
            fn bit_find_first_zero_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                Bitwise(self).find_first_zero_checked_range(start, end)
            }

            /* find last */

            fn bit_find_last_one_range(self, start: u32, end: u32) -> Option<u32> {
                Bitwise(self).find_last_one_range(start, end)
            }
            fn bit_find_last_one_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                Bitwise(self).find_last_one_checked_range(start, end)
            }
            fn bit_find_last_zero_range(self, start: u32, end: u32) -> Option<u32> {
                Bitwise(self).find_last_zero_range(start, end)
            }
            fn bit_find_last_zero_checked_range(self, start: u32, end: u32)
                -> Result<Option<u32>, MismatchedBounds> {
                Bitwise(self).find_last_zero_checked_range(start, end)
            }
        }
    };
}
impl_bit_ops![];