const_serialize/const_buffers.rs
1/// A buffer that can be read from at compile time. This is very similar to [Cursor](std::io::Cursor) but is
2/// designed to be used in const contexts.
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct ConstReadBuffer<'a> {
5 location: usize,
6 memory: &'a [u8],
7}
8
9impl<'a> ConstReadBuffer<'a> {
10 /// Create a new buffer from a byte slice
11 pub const fn new(memory: &'a [u8]) -> Self {
12 Self {
13 location: 0,
14 memory,
15 }
16 }
17
18 /// Get the next byte from the buffer. Returns `None` if the buffer is empty.
19 /// This will return the new version of the buffer with the first byte removed.
20 pub const fn get(mut self) -> Option<(Self, u8)> {
21 if self.location >= self.memory.len() {
22 return None;
23 }
24 let value = self.memory[self.location];
25 self.location += 1;
26 Some((self, value))
27 }
28
29 /// Get a reference to the underlying byte slice
30 pub const fn as_ref(&self) -> &[u8] {
31 self.memory
32 }
33}