Skip to main content

Arena

Struct Arena 

Source
pub struct Arena<T> { /* private fields */ }
Expand description

A generational arena allocator.

Provides O(1) allocation, deallocation, and access with generation-based use-after-free detection. All operations are safe Rust.

§Type Parameter

T is the type of values stored in the arena. For the DOM, this will be NodeData.

§Examples

use tinyxml2::arena::Arena;

let mut arena: Arena<String> = Arena::new();

// Allocate
let id = arena.alloc("hello".to_string());
assert_eq!(arena.get(id), Some(&"hello".to_string()));

// Deallocate
let removed = arena.dealloc(id);
assert_eq!(removed, Some("hello".to_string()));

// Stale ID returns None
assert_eq!(arena.get(id), None);

Implementations§

Source§

impl<T> Arena<T>

Source

pub fn new() -> Self

Creates a new, empty arena.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new arena with the specified capacity pre-allocated.

Source

pub fn alloc(&mut self, value: T) -> NodeId

Allocates a new slot and stores the value, returning its NodeId.

If a previously-deallocated slot is available, it will be reused. Otherwise, a new slot is appended.

Time complexity: O(1) amortized.

Source

pub fn dealloc(&mut self, id: NodeId) -> Option<T>

Deallocates the slot identified by id, returning the stored value.

Returns None if the ID is invalid (wrong generation, out of bounds, or already deallocated).

The slot’s generation is incremented so that any remaining copies of this NodeId become stale.

Time complexity: O(1).

Source

pub fn get(&self, id: NodeId) -> Option<&T>

Returns a reference to the value at id, or None if the ID is invalid.

Time complexity: O(1).

Source

pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T>

Returns a mutable reference to the value at id, or None if invalid.

Time complexity: O(1).

Source

pub fn len(&self) -> usize

Returns the number of currently occupied slots.

Source

pub fn is_empty(&self) -> bool

Returns true if no slots are currently occupied.

Source

pub fn capacity(&self) -> usize

Returns the total capacity (occupied + vacant slots).

Source

pub fn clear(&mut self)

Removes all items from the arena and resets all state.

All existing NodeId values become invalid after this call.

Source

pub fn iter(&self) -> ArenaIter<'_, T>

Returns an iterator over all occupied (NodeId, &T) pairs.

Source

pub fn iter_mut(&mut self) -> ArenaIterMut<'_, T>

Returns a mutable iterator over all occupied (NodeId, &mut T) pairs.

Source

pub fn contains(&self, id: NodeId) -> bool

Returns true if the given NodeId currently refers to a live entry.

Trait Implementations§

Source§

impl<T: Debug> Debug for Arena<T>

Source§

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

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

impl<T> Default for Arena<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> IntoIterator for &'a Arena<T>

Source§

type Item = (NodeId, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = ArenaIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Arena<T>

Source§

type Item = (NodeId, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = ArenaIterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Arena<T>

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for Arena<T>

§

impl<T> UnwindSafe for Arena<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.