Replace move_to_val with from_entry

This commit is contained in:
Andrew Morris
2023-10-30 10:07:33 +11:00
parent 7ac3e65ed3
commit 3db7b96e4b
3 changed files with 23 additions and 39 deletions

View File

@@ -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
}
}

View File

@@ -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)
}

View File

@@ -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)")
}