pub struct VariableList<T> {
pub identity: Vec<usize>,
pub data: Vec<T>,
}Expand description
A variable list provides variable-length unit store.
identity: usize - 1-based integer (0 is the null sentinel). 0 on set appends error: ListError value: [T]
use context_engine::list::VariableList;
use context_engine::required::SetOutcome;
let mut vl: VariableList<u32> = VariableList::new();
// append: first real entry is id=1
let r = vl.set(&0, &[1u32, 2, 3], false).unwrap();
assert!(matches!(r, SetOutcome::Created(1)));
assert_eq!(vl.get(&1).unwrap(), &[1u32, 2, 3]);
// intern: same value returns existing id
let r = vl.set(&0, &[1u32, 2, 3], true).unwrap();
assert!(matches!(r, SetOutcome::Created(1)));
// update in-place (value fits)
let r = vl.set(&1, &[9u32, 8], false).unwrap();
assert!(matches!(r, SetOutcome::Updated));
assert_eq!(vl.get(&1).unwrap(), &[9u32, 8]);
// delete
vl.delete(&1).unwrap();
assert!(vl.get(&1).is_err());Fields§
§identity: Vec<usize>§data: Vec<T>Implementations§
Source§impl<T: Copy + Default + PartialEq> VariableList<T>
impl<T: Copy + Default + PartialEq> VariableList<T>
pub fn get<'a>(&'a self, identity: &usize) -> Result<&'a [T], ListError>
Sourcepub fn set(
&mut self,
identity: &usize,
value: &[T],
intern: bool,
) -> Result<SetOutcome, ListError>
pub fn set( &mut self, identity: &usize, value: &[T], intern: bool, ) -> Result<SetOutcome, ListError>
intern: if true and identity=0, return first match value identity(i)
note: update tries in-place if value fits the existing range; otherwise appends to data and rewrites the identity range (old bytes become unreachable until compact is called).
pub fn delete(&mut self, identity: &usize) -> Result<(), ListError>
Sourcepub fn compact(&mut self) -> Result<BTreeMap<usize, usize>, VariableListError>
pub fn compact(&mut self) -> Result<BTreeMap<usize, usize>, VariableListError>
Rebuilds both identity and data from scratch:
- vacant entries are removed from identity (identity shrinks)
- update-leaked bytes in data are reclaimed
- surviving entries are re-assigned sequential id values starting at 1 Returns a mapping of old id -> new id for callers that hold external references.
use context_engine::list::VariableList;
use context_engine::required::SetOutcome;
let mut vl: VariableList<u32> = VariableList::new();
vl.set(&0, &[1u32, 2, 3], false).unwrap(); // id=1
vl.set(&0, &[4u32, 5, 6], false).unwrap(); // id=2
vl.delete(&1).unwrap(); // id=1 vacant
let remap = vl.compact().unwrap();
assert_eq!(remap[&2], 1); // old id=2 -> new id=1
assert_eq!(vl.get(&1).unwrap(), &[4u32, 5, 6]);Auto Trait Implementations§
impl<T> Freeze for VariableList<T>
impl<T> RefUnwindSafe for VariableList<T>where
T: RefUnwindSafe,
impl<T> Send for VariableList<T>where
T: Send,
impl<T> Sync for VariableList<T>where
T: Sync,
impl<T> Unpin for VariableList<T>where
T: Unpin,
impl<T> UnsafeUnpin for VariableList<T>
impl<T> UnwindSafe for VariableList<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more