mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Replace move_to_val with from_entry
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
storage_ptr::StorageEntryPtr,
|
||||
storage_val::{StorageCompoundVal, StorageVal},
|
||||
};
|
||||
use crate::storage_ptr::StorageEntryPtr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StorageEntry {
|
||||
@@ -13,25 +8,3 @@ pub struct StorageEntry {
|
||||
pub(crate) refs: Vec<StorageEntryPtr>,
|
||||
pub(crate) data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl StorageEntry {
|
||||
pub fn move_to_val(self) -> StorageVal {
|
||||
let Self {
|
||||
ref_count: _,
|
||||
refs,
|
||||
data,
|
||||
} = self;
|
||||
|
||||
let mut val = bincode::deserialize::<StorageVal>(&data).unwrap();
|
||||
|
||||
if let StorageVal::Compound(compound) = &mut val {
|
||||
match Rc::get_mut(compound).expect("Should be single ref") {
|
||||
StorageCompoundVal::Array(arr) => {
|
||||
arr.refs = refs;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ where
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let value = self.read(key)?.map(|entry| entry.move_to_val());
|
||||
let value = self.read(key)?.map(StorageVal::from_entry);
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
@@ -56,6 +56,26 @@ impl StorageVal {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_entry(entry: StorageEntry) -> Self {
|
||||
let StorageEntry {
|
||||
ref_count: _,
|
||||
refs,
|
||||
data,
|
||||
} = entry;
|
||||
|
||||
let mut val = bincode::deserialize::<StorageVal>(&data).unwrap();
|
||||
|
||||
if let StorageVal::Compound(compound) = &mut val {
|
||||
match Rc::get_mut(compound).expect("Should be single ref") {
|
||||
StorageCompoundVal::Array(arr) => {
|
||||
arr.refs = refs;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
val
|
||||
}
|
||||
|
||||
pub fn refs(&self) -> Option<&Vec<StorageEntryPtr>> {
|
||||
match self {
|
||||
StorageVal::Void | StorageVal::Number(_) | StorageVal::Ptr(_) | StorageVal::Ref(_) => None,
|
||||
@@ -156,17 +176,8 @@ impl StorageVal {
|
||||
StorageVal::Number(n) => Ok(vec![*n]),
|
||||
StorageVal::Ptr(ptr) => {
|
||||
let entry = tx.read(*ptr)?.unwrap();
|
||||
entry.move_to_val().numbers_impl(tx)
|
||||
Self::from_entry(entry).numbers_impl(tx)
|
||||
}
|
||||
// StorageVal::Array(arr) => {
|
||||
// let mut numbers = Vec::new();
|
||||
|
||||
// for point in arr.iter() {
|
||||
// numbers.extend(point.numbers(tx, refs)?);
|
||||
// }
|
||||
|
||||
// Ok(numbers)
|
||||
// }
|
||||
StorageVal::Ref(_) => {
|
||||
panic!("Can't lookup ref (shouldn't hit this case)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user