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
use super::{Format, Packable};
use core::{iter, marker::PhantomData};

impl Packable for () {
    fn pack<T>(&self, _buf: &mut T) -> usize
    where
        T: Extend<u8>,
    {
        0
    }
}

impl<X> Packable for PhantomData<X> {
    fn pack<T>(&self, _buf: &mut T) -> usize
    where
        T: Extend<u8>,
    {
        0
    }
}

impl Packable for bool {
    fn pack<T>(&self, buf: &mut T) -> usize
    where
        T: Extend<u8>,
    {
        if *self {
            buf.extend(iter::once(Format::TRUE));
        } else {
            buf.extend(iter::once(Format::FALSE));
        }
        1
    }
}

impl<X> Packable for Option<X>
where
    X: Packable,
{
    fn pack<T>(&self, buf: &mut T) -> usize
    where
        T: Extend<u8>,
    {
        match self {
            Some(t) => t.pack(buf),
            None => {
                buf.extend(iter::once(Format::NIL));
                1
            }
        }
    }
}