Skip to main content

VariableList

Struct VariableList 

Source
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>

Source

pub fn new() -> Self

Source§

impl<T: Copy + Default + PartialEq> VariableList<T>

Source

pub fn get<'a>(&'a self, identity: &usize) -> Result<&'a [T], ListError>

Source

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).

Source

pub fn delete(&mut self, identity: &usize) -> Result<(), ListError>

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.