tree_buf/internal/types/
boolean.rs

1use crate::internal::encodings::packed_bool::*;
2use crate::internal::encodings::rle_bool::*;
3use crate::prelude::*;
4use std::vec::IntoIter;
5
6#[cfg(feature = "encode")]
7impl Encodable for bool {
8    type EncoderArray = Vec<bool>;
9    #[inline]
10    fn encode_root<O: EncodeOptions>(&self, _stream: &mut EncoderStream<'_, O>) -> RootTypeId {
11        if *self {
12            RootTypeId::True
13        } else {
14            RootTypeId::False
15        }
16    }
17}
18
19#[cfg(feature = "decode")]
20impl Decodable for bool {
21    type DecoderArray = IntoIter<bool>;
22    fn decode(sticks: DynRootBranch<'_>, _options: &impl DecodeOptions) -> DecodeResult<Self> {
23        profile_method!(decode);
24        match sticks {
25            DynRootBranch::Boolean(v) => Ok(v),
26            _ => Err(DecodeError::SchemaMismatch),
27        }
28    }
29}
30
31#[cfg(feature = "encode")]
32impl EncoderArray<bool> for Vec<bool> {
33    fn buffer_one<'a, 'b: 'a>(&'a mut self, value: &'b bool) {
34        self.push(*value);
35    }
36    fn buffer_many<'a, 'b: 'a>(&'a mut self, values: &'b [bool]) {
37        profile_method!(buffer_many);
38        self.extend_from_slice(values);
39    }
40    fn encode_all<O: EncodeOptions>(values: &[bool], stream: &mut EncoderStream<'_, O>) -> ArrayTypeId {
41        profile_method!(encode_all);
42
43        // See also 42d5f4b4-823f-4ab4-8448-6e1a341ff28b
44        let compressors = (PackedBoolCompressor, RLEBoolCompressor);
45        compress(values, stream, &compressors)
46    }
47    fn flush<O: EncodeOptions>(self, stream: &mut EncoderStream<'_, O>) -> ArrayTypeId {
48        Self::encode_all(&self[..], stream)
49    }
50}
51
52impl PrimitiveEncoderArray<bool> for Vec<bool> {
53    fn fast_size_for_all<O: EncodeOptions>(values: &[bool], options: &O) -> usize {
54        // See also 42d5f4b4-823f-4ab4-8448-6e1a341ff28b
55        let compressors = (PackedBoolCompressor, RLEBoolCompressor);
56        fast_size_for(values, &compressors, options)
57    }
58}
59
60struct PackedBoolCompressor;
61impl Compressor<bool> for PackedBoolCompressor {
62    fn fast_size_for<O: EncodeOptions>(&self, data: &[bool], _options: &O) -> Result<usize, ()> {
63        let buffer_len = (data.len() + 7) / 8;
64        let len_len = size_for_varint(buffer_len as u64);
65        Ok(buffer_len + len_len)
66    }
67    fn compress<O: EncodeOptions>(&self, data: &[bool], stream: &mut EncoderStream<'_, O>) -> Result<ArrayTypeId, ()> {
68        profile_method!(compress);
69        stream.encode_with_len(|stream| encode_packed_bool(data, stream.bytes));
70        Ok(ArrayTypeId::PackedBool)
71    }
72}
73
74struct RLEBoolCompressor;
75
76impl Compressor<bool> for RLEBoolCompressor {
77    // TODO: fast_size_for
78    fn compress<O: EncodeOptions>(&self, data: &[bool], stream: &mut EncoderStream<'_, O>) -> Result<ArrayTypeId, ()> {
79        within_rle(|| encode_rle_bool(data, stream))
80    }
81    fn fast_size_for<O: EncodeOptions>(&self, data: &[bool], options: &O) -> Result<usize, ()> {
82        within_rle(|| size_of_rle_bool(data, options))
83    }
84}
85
86#[cfg(feature = "decode")]
87impl InfallibleDecoderArray for IntoIter<bool> {
88    type Decode = bool;
89
90    fn new_infallible(sticks: DynArrayBranch<'_>, options: &impl DecodeOptions) -> DecodeResult<Self> {
91        profile_method!(new_infallible);
92
93        match sticks {
94            DynArrayBranch::Boolean(encoding) => {
95                let v = match encoding {
96                    ArrayBool::Packed(bytes) => decode_packed_bool(&bytes).into_iter(),
97                    ArrayBool::RLE(first, runs) => {
98                        let runs = <u64 as Decodable>::DecoderArray::new(*runs, options)?;
99                        decode_rle_bool(runs, first)
100                    }
101                };
102                Ok(v)
103            }
104            _ => Err(DecodeError::SchemaMismatch),
105        }
106    }
107    fn decode_next_infallible(&mut self) -> Self::Decode {
108        self.next().unwrap_or_default()
109    }
110}