bitbuffer 0.11.1

Reading bit sequences from a byte slice
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
//! some extra number traits

use crate::Endianness;
use num_traits::{PrimInt, WrappingSub};
use std::array::TryFromSliceError;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::ops::{BitOrAssign, BitXor};

/// Allow casting floats unchecked
pub trait UncheckedPrimitiveFloat: Sized {
    /// Byte array of the size of the float
    type BYTES: AsRef<[u8]> + for<'a> TryFrom<&'a [u8], Error = TryFromSliceError>;
    /// The corresponding int of the same size
    type INT: PrimInt
        + BitOrAssign
        + IsSigned
        + UncheckedPrimitiveInt
        + BitXor
        + Debug
        + SplitFitUsize
        + WrappingSub;

    /// Cast from f32
    fn from_f32_unchecked(n: f32) -> Self;
    /// Cast from f64, truncating if needed
    fn from_f64_unchecked(n: f64) -> Self;
    /// Cast a float to byte array
    fn to_bytes<E: Endianness>(self) -> Self::BYTES;
    /// Cast a byte array to float
    fn from_bytes<E: Endianness>(bytes: Self::BYTES) -> Self;
    /// Cast a float to an int with the same size
    fn to_int(self) -> Self::INT;
    /// Cast an integer to a float with the same size
    fn from_int(int: Self::INT) -> Self;
}

impl UncheckedPrimitiveFloat for f32 {
    type BYTES = [u8; 4];
    type INT = u32;

    #[inline(always)]
    fn from_f32_unchecked(n: f32) -> Self {
        n
    }
    #[inline(always)]
    fn from_f64_unchecked(n: f64) -> Self {
        n as f32
    }
    fn to_bytes<E: Endianness>(self) -> Self::BYTES {
        if E::is_le() {
            self.to_le_bytes()
        } else {
            self.to_be_bytes()
        }
    }
    fn from_bytes<E: Endianness>(bytes: Self::BYTES) -> Self {
        if E::is_le() {
            Self::from_le_bytes(bytes)
        } else {
            Self::from_be_bytes(bytes)
        }
    }
    fn to_int(self) -> Self::INT {
        Self::INT::from_le_bytes(self.to_le_bytes())
    }
    fn from_int(int: Self::INT) -> Self {
        Self::from_le_bytes(int.to_le_bytes())
    }
}

impl UncheckedPrimitiveFloat for f64 {
    type BYTES = [u8; 8];
    type INT = u64;

    #[inline(always)]
    fn from_f32_unchecked(n: f32) -> Self {
        n as f64
    }
    #[inline(always)]
    fn from_f64_unchecked(n: f64) -> Self {
        n
    }
    fn to_bytes<E: Endianness>(self) -> Self::BYTES {
        if E::is_le() {
            self.to_le_bytes()
        } else {
            self.to_be_bytes()
        }
    }
    fn from_bytes<E: Endianness>(bytes: Self::BYTES) -> Self {
        if E::is_le() {
            Self::from_le_bytes(bytes)
        } else {
            Self::from_be_bytes(bytes)
        }
    }
    fn to_int(self) -> Self::INT {
        Self::INT::from_le_bytes(self.to_le_bytes())
    }
    fn from_int(int: Self::INT) -> Self {
        Self::from_le_bytes(int.to_le_bytes())
    }
}

/// Allow casting integers unchecked
pub trait UncheckedPrimitiveInt: Sized {
    /// Cast from u8, truncating if needed
    fn from_u8_unchecked(n: u8) -> Self;
    /// Cast from i8, truncating if needed
    fn from_i8_unchecked(n: i8) -> Self;
    /// Cast from u16, truncating if needed
    fn from_u16_unchecked(n: u16) -> Self;
    /// Cast from i16, truncating if needed
    fn from_i16_unchecked(n: i16) -> Self;
    /// Cast from u32, truncating if needed
    fn from_u32_unchecked(n: u32) -> Self;
    /// Cast from i32, truncating if needed
    fn from_i32_unchecked(n: i32) -> Self;
    /// Cast from u64, truncating if needed
    fn from_u64_unchecked(n: u64) -> Self;
    /// Cast from i64, truncating if needed
    fn from_i64_unchecked(n: i64) -> Self;
    /// Cast from u128, truncating if needed
    fn from_u128_unchecked(n: u128) -> Self;
    /// Cast from i128, truncating if needed
    fn from_i128_unchecked(n: i128) -> Self;
    /// Cast from usize, truncating if needed
    fn from_usize_unchecked(n: usize) -> Self;
    /// Cast from isize, truncating if needed
    fn from_isize_unchecked(n: isize) -> Self;

    /// Cast from u8, truncating if needed
    fn into_u8_unchecked(self) -> u8;
    /// Cast from i8, truncating if needed
    fn into_i8_unchecked(self) -> i8;
    /// Cast from u16, truncating if needed
    fn into_u16_unchecked(self) -> u16;
    /// Cast from i16, truncating if needed
    fn into_i16_unchecked(self) -> i16;
    /// Cast from u32, truncating if needed
    fn into_u32_unchecked(self) -> u32;
    /// Cast from i32, truncating if needed
    fn into_i32_unchecked(self) -> i32;
    /// Cast from u64, truncating if needed
    fn into_u64_unchecked(self) -> u64;
    /// Cast from i64, truncating if needed
    fn into_i64_unchecked(self) -> i64;
    /// Cast from u128, truncating if needed
    fn into_u128_unchecked(self) -> u128;
    /// Cast from i128, truncating if needed
    fn into_i128_unchecked(self) -> i128;
    /// Cast from usize, truncating if needed
    fn into_usize_unchecked(self) -> usize;
    /// Cast from isize, truncating if needed
    fn into_isize_unchecked(self) -> isize;

    /// Cast any int to any int, truncating if needed
    fn from_unchecked<N: UncheckedPrimitiveInt>(n: N) -> Self;
}

macro_rules! impl_unchecked_int {
    ($type:ty, $conv:ident) => {
        impl UncheckedPrimitiveInt for $type {
            #[inline(always)]
            fn from_u8_unchecked(n: u8) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_i8_unchecked(n: i8) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_u16_unchecked(n: u16) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_i16_unchecked(n: i16) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_u32_unchecked(n: u32) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_i32_unchecked(n: i32) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_u64_unchecked(n: u64) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_i64_unchecked(n: i64) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_u128_unchecked(n: u128) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_i128_unchecked(n: i128) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_usize_unchecked(n: usize) -> Self {
                n as $type
            }
            #[inline(always)]
            fn from_isize_unchecked(n: isize) -> Self {
                n as $type
            }

            fn into_u8_unchecked(self) -> u8 {
                self as u8
            }
            #[inline(always)]
            fn into_i8_unchecked(self) -> i8 {
                self as i8
            }
            #[inline(always)]
            fn into_u16_unchecked(self) -> u16 {
                self as u16
            }
            #[inline(always)]
            fn into_i16_unchecked(self) -> i16 {
                self as i16
            }
            #[inline(always)]
            fn into_u32_unchecked(self) -> u32 {
                self as u32
            }
            #[inline(always)]
            fn into_i32_unchecked(self) -> i32 {
                self as i32
            }
            #[inline(always)]
            fn into_u64_unchecked(self) -> u64 {
                self as u64
            }
            #[inline(always)]
            fn into_i64_unchecked(self) -> i64 {
                self as i64
            }
            #[inline(always)]
            fn into_u128_unchecked(self) -> u128 {
                self as u128
            }
            #[inline(always)]
            fn into_i128_unchecked(self) -> i128 {
                self as i128
            }
            #[inline(always)]
            fn into_usize_unchecked(self) -> usize {
                self as usize
            }
            #[inline(always)]
            fn into_isize_unchecked(self) -> isize {
                self as isize
            }

            #[inline(always)]
            fn from_unchecked<N: UncheckedPrimitiveInt>(n: N) -> Self {
                n.$conv()
            }
        }
    };
}

impl_unchecked_int!(u8, into_u8_unchecked);
impl_unchecked_int!(i8, into_i8_unchecked);
impl_unchecked_int!(u16, into_u16_unchecked);
impl_unchecked_int!(i16, into_i16_unchecked);
impl_unchecked_int!(u32, into_u32_unchecked);
impl_unchecked_int!(i32, into_i32_unchecked);
impl_unchecked_int!(u64, into_u64_unchecked);
impl_unchecked_int!(i64, into_i64_unchecked);
impl_unchecked_int!(u128, into_u128_unchecked);
impl_unchecked_int!(i128, into_i128_unchecked);
impl_unchecked_int!(usize, into_usize_unchecked);
impl_unchecked_int!(isize, into_isize_unchecked);

/// Check if an integer type is signed
pub trait IsSigned {
    /// Check if the integer type is signed
    fn is_signed() -> bool;
}

macro_rules! impl_is_signed {
    ($type:ty, $signed:expr) => {
        impl IsSigned for $type {
            #[inline(always)]
            fn is_signed() -> bool {
                $signed
            }
        }
    };
}

impl_is_signed!(u8, false);
impl_is_signed!(u16, false);
impl_is_signed!(u32, false);
impl_is_signed!(u64, false);
impl_is_signed!(u128, false);
impl_is_signed!(usize, false);
impl_is_signed!(i8, true);
impl_is_signed!(i16, true);
impl_is_signed!(i32, true);
impl_is_signed!(i64, true);
impl_is_signed!(i128, true);
impl_is_signed!(isize, true);

/// Split an integer into chunks that are smaller than a `usize`
pub trait SplitFitUsize {
    /// Integer of integer chunks
    type Iter: Iterator<Item = (usize, u8)> + ExactSizeIterator + DoubleEndedIterator;

    /// Split a `count` bit integer into chunks that are smaller than a `usize`
    fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter;
}

use std::array;
use std::mem::size_of;

macro_rules! impl_split_fit {
    ($type:ty) => {
        impl SplitFitUsize for $type {
            type Iter = array::IntoIter<(usize, u8), 1>;

            fn split_fit_usize<E: Endianness>(self, _count: u8) -> Self::Iter {
                assert!(size_of::<Self>() < size_of::<usize>());
                [(self as usize, size_of::<Self>() as u8 * 8)].into_iter()
            }
        }
    };
}

macro_rules! impl_split_fit_signed {
    ($signed_type:ty, $unsigned_type:ty) => {
        impl SplitFitUsize for $signed_type {
            type Iter = <$unsigned_type as SplitFitUsize>::Iter;

            fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter {
                let unsigned = <$unsigned_type>::from_ne_bytes(self.to_ne_bytes());
                unsigned.split_fit_usize::<E>(count)
            }
        }
    };
}

impl_split_fit!(u8);
impl_split_fit!(u16);
impl_split_fit!(i8);
impl_split_fit!(i16);
#[cfg(target_pointer_width = "64")]
impl_split_fit!(u32);

#[cfg(target_pointer_width = "32")]
impl SplitFitUsize for u32 {
    type Iter = array::IntoIter<(usize, u8), 2>;

    fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter {
        Self::Iter::new(if E::is_le() {
            [
                ((self & (Self::MAX >> 8)) as usize, 24),
                ((self >> 24) as usize, 8),
            ]
        } else {
            let offset = Self::BITS as u8 - count;
            [
                ((self >> 24) as usize, 8u8.saturating_sub(offset)),
                (
                    (self & (Self::MAX >> 8)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(8)),
                ),
            ]
        })
    }
}

impl_split_fit_signed!(i32, u32);

impl SplitFitUsize for u64 {
    type Iter = array::IntoIter<(usize, u8), 3>;

    fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter {
        (if E::is_le() {
            [
                ((self & (Self::MAX >> 40)) as usize, 24),
                (((self >> 24) & (Self::MAX >> 16)) as usize, 24),
                ((self >> 48) as usize, 16),
            ]
        } else {
            let offset = Self::BITS as u8 - count;
            [
                ((self >> 48) as usize, 16u8.saturating_sub(offset)),
                (
                    ((self >> 24) & (Self::MAX >> 16)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(16)),
                ),
                (
                    (self & (Self::MAX >> 40)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(40)),
                ),
            ]
        })
        .into_iter()
    }
}

impl_split_fit_signed!(i64, u64);

impl SplitFitUsize for u128 {
    type Iter = array::IntoIter<(usize, u8), 6>;

    fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter {
        (if E::is_le() {
            [
                ((self & (Self::MAX >> 104)) as usize, 24),
                (((self >> 24) & (Self::MAX >> 80)) as usize, 24),
                (((self >> 48) & (Self::MAX >> 56)) as usize, 24),
                (((self >> 72) & (Self::MAX >> 32)) as usize, 24),
                (((self >> 96) & (Self::MAX >> 8)) as usize, 24),
                ((self >> 120) as usize, 8),
            ]
        } else {
            let offset = Self::BITS as u8 - count;
            [
                ((self >> 120) as usize, 8u8.saturating_sub(offset)),
                (
                    ((self >> 96) & (Self::MAX >> 8)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(8)),
                ),
                (
                    ((self >> 72) & (Self::MAX >> 32)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(32)),
                ),
                (
                    ((self >> 48) & (Self::MAX >> 56)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(56)),
                ),
                (
                    ((self >> 24) & (Self::MAX >> 80)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(80)),
                ),
                (
                    (self & (Self::MAX >> 104)) as usize,
                    24u8.saturating_sub(offset.saturating_sub(104)),
                ),
            ]
        })
        .into_iter()
    }
}

impl_split_fit_signed!(i128, u128);

impl SplitFitUsize for usize {
    type Iter = array::IntoIter<(usize, u8), 2>;

    fn split_fit_usize<E: Endianness>(self, count: u8) -> Self::Iter {
        (if E::is_le() {
            [
                (
                    self & (Self::MAX >> (usize::BITS - 8)),
                    usize::BITS as u8 - 8,
                ),
                (self >> (usize::BITS - 8), 8),
            ]
        } else {
            let offset = Self::BITS as u8 - count;
            [
                (self >> (usize::BITS - 8), 8u8.saturating_sub(offset)),
                (
                    self & (Self::MAX >> 8),
                    (usize::BITS as u8 - 8).saturating_sub(offset.saturating_sub(8)),
                ),
            ]
        })
        .into_iter()
    }
}

impl_split_fit_signed!(isize, usize);