density_rs/
buffer.rs

1pub struct Buffer<const N: usize> {
2    pub buffer: [u8; N],
3    pub index: usize,
4}
5
6impl<const N: usize> Buffer<N> {
7    pub fn new() -> Self {
8        Buffer {
9            buffer: [0; N],
10            index: 0,
11        }
12    }
13
14    #[inline(always)]
15    pub fn push(&mut self, bytes: &[u8]) {
16        let future_index = self.index + bytes.len();
17        self.buffer[self.index..future_index].copy_from_slice(bytes);
18        self.index = future_index;
19    }
20
21    #[inline(always)]
22    pub fn remaining_space(&self) -> usize {
23        N - self.index
24    }
25
26    #[inline(always)]
27    pub fn reset(&mut self) {
28        self.index = 0;
29    }
30
31    #[inline(always)]
32    pub fn is_empty(&self) -> bool {
33        self.index == 0
34    }
35}