streamdata/
buffer.rs

1//! Buffer implementations.
2
3/// [`Buffer`] captures the interface we require from the piece that maintains
4/// the [`crate::State`] buffer.
5/// This buffer is intended for keeping the undecoded partial chunks.
6pub trait Buffer {
7    /// Append the data to the end of the buffer.
8    fn append(&mut self, chunk: &[u8]);
9    /// View the current contents of the buffer.
10    fn view(&self) -> &[u8];
11    /// Drop the given amout of bytes from the start of the buffer.
12    fn advance(&mut self, bytes: usize);
13}
14
15impl super::Buffer for Vec<u8> {
16    fn append(&mut self, chunk: &[u8]) {
17        self.extend_from_slice(chunk)
18    }
19
20    fn view(&self) -> &[u8] {
21        self
22    }
23
24    fn advance(&mut self, bytes: usize) {
25        self.drain(..bytes);
26    }
27}
28
29#[cfg(feature = "bytes")]
30impl super::Buffer for bytes::BytesMut {
31    fn append(&mut self, chunk: &[u8]) {
32        bytes::BufMut::put_slice(self, chunk)
33    }
34
35    fn view(&self) -> &[u8] {
36        bytes::Buf::chunk(self)
37    }
38
39    fn advance(&mut self, bytes: usize) {
40        bytes::Buf::advance(self, bytes)
41    }
42}