q_compress/data_types/
boolean.rs

1use std::convert::TryInto;
2
3use crate::data_types::{NumberLike, SignedLike};
4use crate::errors::QCompressResult;
5
6impl SignedLike for bool {
7  const ZERO: Self = false;
8
9  #[inline]
10  fn wrapping_add(self, other: Self) -> Self {
11    self ^ other
12  }
13
14  #[inline]
15  fn wrapping_sub(self, other: Self) -> Self {
16    self ^ other
17  }
18}
19
20impl NumberLike for bool {
21  const HEADER_BYTE: u8 = 7;
22  // it's easiest to use 8 bits per uncompressed boolean
23  // because that's how rust represents them too
24  const PHYSICAL_BITS: usize = 8;
25
26  type Signed = bool;
27  type Unsigned = u8;
28
29  #[inline]
30  fn to_unsigned(self) -> u8 {
31    self as u8
32  }
33
34  #[inline]
35  fn from_unsigned(off: u8) -> bool {
36    off > 0
37  }
38
39  #[inline]
40  fn to_signed(self) -> bool {
41    self
42  }
43
44  #[inline]
45  fn from_signed(signed: bool) -> bool {
46    signed
47  }
48
49  fn to_bytes(self) -> Vec<u8> {
50    vec![self as u8]
51  }
52
53  fn from_bytes(bytes: &[u8]) -> QCompressResult<bool> {
54    Ok(u8::from_be_bytes(bytes.try_into().unwrap()) != 0)
55  }
56}