Struct NodeField

Source
pub struct NodeField<T: Clone> { /* private fields */ }
Expand description

Used to allocate space on the heap, read from that space, and write to it.

Implementations§

Source§

impl<T: Clone> NodeField<T>

Source

pub fn new() -> Self

Constructs a new NodeField which can store data of type T

§Example
//Stores i32s
let mut storage = NodeField::<i32>::new();
Source

pub fn data<I: Indexable>(&self, index: I) -> Result<&T, AccessError>

Returns an immutable reference to the data stored at the requested index, or an AccessError if there is a problem.

Source

pub fn mut_data<I: Indexable>( &mut self, index: I, ) -> Result<&mut T, AccessError>

Returns a mutable reference to the data stored at the requested index, or an AccessError if there is a problem.

Source

pub fn add_ref<I: Indexable>(&mut self, index: I) -> Result<(), AccessError>

Tells the NodeField that something else references the data at index. So long as the NodeField thinks there is at least one reference, the data won’t be freed.

Failure to properly track references will lead to either freeing data you wanted or leaking data you didn’t.

Source

pub fn remove_ref<I: Indexable>( &mut self, index: I, ) -> Result<Option<T>, AccessError>

Tells the NodeField that something no longer references the data at index. If calling this function renders the refcount 0, the data will be freed and returned.

Failure to properly track references will lead to either freeing data you wanted or leaking data you didn’t.

Source

pub fn status<I: Indexable>(&self, index: I) -> Result<usize, AccessError>

Returns the number of references the data at index has or an AccessError if the request has a problem

Source

pub fn push(&mut self, data: T) -> usize

Pushes data into the NodeField, returning the index it was stored at.

Once you recieve the index the data was stored at, it is your responsibility to manage its references. The data will start with one reference.

Source

pub fn replace<I: Indexable>( &mut self, index: I, new_data: T, ) -> Result<T, AccessError>

Replaces the data at index with new_data, returning the replaced data on success and an AccessError on failure. You may not replace an index which is currently free.

Source

pub fn next_allocated(&self) -> usize

Returns the next index which will be allocated on a NodeField::push call

Source

pub fn repair_allocator(&mut self)

Travels through each slot in the vec checks whether it currently contains data, updating the allocator accordingly.

Source

pub fn defrag(&mut self) -> HashMap<usize, usize>

Travels through memory and re-arranges slots so that they are contiguous in memory, with no free slots in between occupied ones. The hashmap returned can be used to remap your references to their new locations. (Key:Old, Value:New)

Slots at the back of memory will be placed in the first free slot, until the above condition is met.

This operation is O(n) to the number of slots in memory.

Source

pub fn trim(&mut self) -> HashMap<usize, usize>

NodeField::defrags the memory, then shrinks the internal memory Vec to the size of the block of occupied memory.

Source

pub fn allocation_map(&self) -> &Vec<usize>

Returns a reference to the current bitfield of allocated/free memory slots

Source

pub fn memory(&self) -> &Vec<Option<Steward<T>>>

Returns a reference to the internal memory Vec

Trait Implementations§

Source§

impl<T: Debug + Clone> Debug for NodeField<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for NodeField<T>
where T: Deserialize<'de> + Clone,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Serialize for NodeField<T>
where T: Serialize + Clone,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for NodeField<T>

§

impl<T> RefUnwindSafe for NodeField<T>
where T: RefUnwindSafe,

§

impl<T> Send for NodeField<T>
where T: Send,

§

impl<T> Sync for NodeField<T>
where T: Sync,

§

impl<T> Unpin for NodeField<T>
where T: Unpin,

§

impl<T> UnwindSafe for NodeField<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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,