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
/// Trait representing a buffer that protocol messages can be read from
pub trait InputBuffer {
    /// Retrieve all current data in the buffer
    fn data(&self) -> &[u8];
    /// Remove `count` bytes from the front of the buffer
    fn pop(&mut self, count: usize);
    /// Retrieve the amount of data currently in the buffer
    fn available(&self) -> usize {
        self.data().len()
    }
}

/// An `InputBuffer` implementation wrapping a slice
pub struct SliceInputBuffer<'a> {
    buffer: &'a [u8],
}

impl<'a> SliceInputBuffer<'a> {
    /// Create a new `SliceInputBuffer` backed by an input byte slice
    pub fn new(buffer: &'a [u8]) -> Self {
        Self { buffer }
    }
}

impl<'a> InputBuffer for SliceInputBuffer<'a> {
    fn data(&self) -> &[u8] {
        self.buffer
    }

    fn pop(&mut self, count: usize) {
        let count = count.clamp(0, self.buffer.len());
        self.buffer = &self.buffer[count..];
    }
}

#[cfg(feature = "std")]
impl InputBuffer for Vec<u8> {
    fn data(&self) -> &[u8] {
        &self[..]
    }

    fn pop(&mut self, count: usize) {
        self.splice(0..count, std::iter::empty());
    }

    fn available(&self) -> usize {
        self.len()
    }
}