extendable_vm/parsing/
raw_bytes.rs

1use crate::byte_readable::ByteReadable;
2use std::fs;
3use std::io::Error;
4
5/// The current reading location of `RawBytes`
6pub struct RawBytesPointer {
7    pub next_byte: usize,
8}
9
10impl RawBytesPointer {
11    pub fn new() -> RawBytesPointer {
12        RawBytesPointer { next_byte: 0 }
13    }
14}
15
16impl Default for RawBytesPointer {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22/// A vector of bytes
23pub struct RawBytes {
24    data: Vec<u8>,
25}
26
27impl RawBytes {
28    pub fn from_file(path: &str) -> Result<RawBytes, Error> {
29        let data = fs::read(path)?;
30        Ok(RawBytes { data })
31    }
32    pub fn from_bytes(bytes: Vec<u8>) -> RawBytes {
33        RawBytes { data: bytes }
34    }
35}
36
37impl ByteReadable<RawBytesPointer> for RawBytes {
38    fn read(&self, ptr: &mut RawBytesPointer) -> Option<u8> {
39        let result = self.data.get(ptr.next_byte).cloned();
40        ptr.next_byte += 1;
41        result
42    }
43
44    fn has_next(&self, ptr: &RawBytesPointer) -> bool {
45        ptr.next_byte < self.data.len()
46    }
47}