bitcode/
bool.rs

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
use crate::coder::{Buffer, Decoder, Encoder, Result, View};
use crate::fast::{CowSlice, NextUnchecked, PushUnchecked, SliceImpl, Unaligned, VecImpl};
use crate::pack::{pack_bools, unpack_bools};
use alloc::vec::Vec;
use core::num::NonZeroUsize;

#[derive(Default)]
pub struct BoolEncoder(VecImpl<bool>);

impl Encoder<bool> for BoolEncoder {
    #[inline(always)]
    fn as_primitive(&mut self) -> Option<&mut VecImpl<bool>> {
        Some(&mut self.0)
    }

    #[inline(always)]
    fn encode(&mut self, t: &bool) {
        unsafe { self.0.push_unchecked(*t) };
    }
}

impl Buffer for BoolEncoder {
    fn collect_into(&mut self, out: &mut Vec<u8>) {
        pack_bools(self.0.as_slice(), out);
        self.0.clear();
    }

    fn reserve(&mut self, additional: NonZeroUsize) {
        self.0.reserve(additional.get());
    }
}

#[derive(Default)]
pub struct BoolDecoder<'a>(CowSlice<'a, bool>);

impl<'a> View<'a> for BoolDecoder<'a> {
    fn populate(&mut self, input: &mut &'_ [u8], length: usize) -> Result<()> {
        unpack_bools(input, length, &mut self.0)?;
        Ok(())
    }
}

impl<'a> Decoder<'a, bool> for BoolDecoder<'a> {
    #[inline(always)]
    fn as_primitive(&mut self) -> Option<&mut SliceImpl<Unaligned<bool>>> {
        // Safety: `Unaligned<bool>` is equivalent to bool since it's a `#[repr(C, packed)]` wrapper
        // around bool and both have size/align of 1.
        unsafe { Some(core::mem::transmute(self.0.mut_slice())) }
    }

    #[inline(always)]
    fn decode(&mut self) -> bool {
        unsafe { self.0.mut_slice().next_unchecked() }
    }
}

#[cfg(test)]
mod test {
    use alloc::vec::Vec;

    fn bench_data() -> Vec<bool> {
        (0..=1000).map(|_| false).collect()
    }
    crate::bench_encode_decode!(bool_vec: Vec<_>);
}

#[cfg(test)]
mod test2 {
    use alloc::vec::Vec;

    fn bench_data() -> Vec<Vec<bool>> {
        crate::random_data::<u8>(125)
            .into_iter()
            .map(|n| {
                let n = 1 + n / 16;
                (0..n).map(|_| false).collect()
            })
            .collect()
    }
    crate::bench_encode_decode!(bool_vecs: Vec<Vec<_>>);
}