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
pub mod reader;
pub mod writer;

pub trait VariableReadable {
    type Error;

    fn read_single(&mut self) -> Result<u8, Self::Error>;

    fn read_more(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
        for i in 0..buf.len() {
            buf[i] = self.read_single()?;
        }
        Ok(())
    }

    #[cfg(feature = "bytes")]
    #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
    fn read_more_buf<B: bytes::BufMut>(&mut self, buf: &mut B) -> Result<(), Self::Error> {
        use bytes::BufMut;
        while buf.has_remaining_mut() {
            let chunk = buf.chunk_mut();
            let chunk = unsafe {&mut *core::ptr::slice_from_raw_parts_mut(chunk.as_mut_ptr(), chunk.len()) };
            self.read_more(chunk)?;
        }
        Ok(())
    }
}

pub trait VariableWritable {
    type Error;

    fn write_single(&mut self, byte: u8) -> Result<(), Self::Error>;

    fn write_more(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        for i in 0..buf.len() {
            self.write_single(buf[i])?;
        }
        Ok(())
    }

    #[cfg(feature = "bytes")]
    #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
    fn write_more_buf<B: bytes::Buf>(&mut self, buf: &mut B) -> Result<(), Self::Error> {
        use bytes::Buf;
        while buf.has_remaining() {
            let chunk = buf.chunk();
            self.write_more(chunk)?;
            buf.advance(chunk.len());
        }
        Ok(())
    }
}