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
use ::core::primitive::bool;
use crate::TBytes;
impl TBytes for bool {
fn size(&self) -> usize {
1
}
fn to_bytes(&self) -> Vec<u8> {
if *self {
vec![1]
} else {
vec![0]
}
}
fn from_bytes(buffer: &mut Vec<u8>) -> Option<Self>
where
Self: Sized,
{
let byte = buffer.pop()?;
if byte > 0 {
Some(true)
} else {
Some(false)
}
}
}
#[cfg(test)]
mod test {
use crate::TBytes;
#[test]
fn bool() {
let a = true;
let mut bytes = a.to_bytes();
bytes.reverse();
let other = bool::from_bytes(&mut bytes).unwrap();
assert_eq!(a, other);
let b = true;
let mut bytes = b.to_bytes();
bytes.reverse();
let other = bool::from_bytes(&mut bytes).unwrap();
assert_eq!(b, other)
}
}