mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Read most vals from storage
This commit is contained in:
@@ -10,6 +10,8 @@ pub trait StorageBackendHandle<'a, E> {
|
||||
fn cache(&mut self) -> &mut HashMap<RcKey, StorageEntryPtr>;
|
||||
fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, E>;
|
||||
fn write_bytes<T>(&mut self, ptr: StoragePtr<T>, data: Option<Vec<u8>>) -> Result<(), E>;
|
||||
|
||||
// TODO: StorageOps should just be methods here
|
||||
}
|
||||
|
||||
pub trait StorageBackend {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::storage_ptr::StorageEntryPtr;
|
||||
@@ -24,34 +26,75 @@ impl<'a> StorageEntryReader<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_ref(&mut self) -> Option<StorageEntryPtr> {
|
||||
pub fn read_ref(&mut self) -> std::io::Result<StorageEntryPtr> {
|
||||
if self.refs_i >= self.entry.refs.len() {
|
||||
return None;
|
||||
return Err(eof());
|
||||
}
|
||||
|
||||
let ptr = self.entry.refs[self.refs_i];
|
||||
self.refs_i += 1;
|
||||
Some(ptr)
|
||||
Ok(ptr)
|
||||
}
|
||||
|
||||
pub fn read_u8(&mut self) -> Option<u8> {
|
||||
pub fn read_u8(&mut self) -> std::io::Result<u8> {
|
||||
if self.data_i >= self.entry.data.len() {
|
||||
return None;
|
||||
return Err(eof());
|
||||
}
|
||||
|
||||
let byte = self.entry.data[self.data_i];
|
||||
self.data_i += 1;
|
||||
Some(byte)
|
||||
Ok(byte)
|
||||
}
|
||||
|
||||
pub fn read_u64(&mut self) -> Option<u64> {
|
||||
if self.data_i + 8 > self.entry.data.len() {
|
||||
return None;
|
||||
pub fn peek_u8(&self) -> std::io::Result<u8> {
|
||||
if self.data_i >= self.entry.data.len() {
|
||||
return Err(eof());
|
||||
}
|
||||
|
||||
let bytes = self.entry.data.get(self.data_i..self.data_i + 8)?;
|
||||
self.data_i += 8;
|
||||
Ok(self.entry.data[self.data_i])
|
||||
}
|
||||
|
||||
Some(u64::from_le_bytes(bytes.try_into().unwrap()))
|
||||
pub fn read_u64(&mut self) -> std::io::Result<u64> {
|
||||
let mut bytes = [0; 8];
|
||||
self.read_exact(&mut bytes)?;
|
||||
Ok(u64::from_le_bytes(bytes))
|
||||
}
|
||||
|
||||
pub fn read_vlq(&mut self) -> std::io::Result<u64> {
|
||||
let mut result = 0;
|
||||
let mut shift = 0;
|
||||
|
||||
loop {
|
||||
let byte = self.read_u8()?;
|
||||
|
||||
result |= ((byte & 0x7f) as u64) << shift;
|
||||
|
||||
if byte & 0x80 == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for StorageEntryReader<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let bytes = self
|
||||
.entry
|
||||
.data
|
||||
.get(self.data_i..self.data_i + buf.len())
|
||||
.ok_or(eof())?;
|
||||
|
||||
buf.copy_from_slice(bytes);
|
||||
self.data_i += buf.len();
|
||||
|
||||
Ok(buf.len())
|
||||
}
|
||||
}
|
||||
|
||||
fn eof() -> std::io::Error {
|
||||
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "not enough bytes")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user