1// Take a look at the license at the top of the repository in the LICENSE file.
2
3const ARRAY_DEFAULT: u8 = 0;
4
5#[derive(Debug)]
6pub struct Buffer {
7 buffer: Vec<u8>,
8 read_pos: usize,
9 buffer_size: usize,
10 data_size: usize,
11}
12
13impl Buffer {
14 pub fn new(size: usize) -> Buffer {
15 Buffer {
16 buffer: vec![ARRAY_DEFAULT; size],
17 read_pos: 0,
18 buffer_size: size,
19 data_size: 0,
20 }
21 }
22
23 pub fn as_mut(&mut self) -> &mut [u8] {
24 self.buffer.as_mut()
25 }
26
27 pub fn update_metadata(&mut self, size: usize) {
28 self.read_pos = 0;
29 self.data_size = size;
30 }
31
32 pub fn next(&mut self) -> Option<u8> {
33 if self.read_pos >= self.data_size {
34 return None;
35 }
36 let item = self.buffer.get(self.read_pos);
37 self.read_pos += 1;
38 item.copied()
39 }
40
41 pub fn cont(&self) -> bool {
42 self.data_size == self.buffer_size
43 }
44}
45