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
use crate::{NumBytes, Read, ReadError, Write, WriteError};

#[derive(Clone, Default, Debug)]
pub struct BinaryExtension<T>(Option<T>);

impl<T> NumBytes for BinaryExtension<T>
where
    T: NumBytes,
{
    fn num_bytes(&self) -> usize {
        self.0.as_ref().map(|t| t.num_bytes()).unwrap_or_default()
    }
}

impl<T> Read for BinaryExtension<T>
where
    T: Read,
{
    #[inline]
    fn read(bytes: &[u8], pos: &mut usize) -> Result<Self, ReadError> {
        if bytes.len() - *pos > 0 {
            T::read(bytes, pos).map(|t| Self(Some(t)))
        } else {
            Ok(Self(None))
        }
    }
}

impl<T> Write for BinaryExtension<T>
where
    T: Write,
{
    #[inline]
    fn write(
        &self,
        bytes: &mut [u8],
        pos: &mut usize,
    ) -> Result<(), WriteError> {
        match &self.0 {
            Some(t) => t.write(bytes, pos),
            None => Ok(()),
        }
    }
}

impl<T> BinaryExtension<T> {
    #[inline]
    pub const fn new(t: Option<T>) -> Self {
        Self(t)
    }

    #[inline]
    pub fn as_value(&self) -> Option<&T> {
        self.0.as_ref()
    }

    #[allow(clippy::missing_const_for_fn)]
    #[inline]
    pub fn into_value(self) -> Option<T> {
        self.0
    }
}