Arena

Struct Arena 

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

An Arena structure containing certain Nodes.

Implementations§

Source§

impl<T> Arena<T>

Source

pub fn new() -> Arena<T>

Creates a new empty Arena.

Source

pub fn with_capacity(n: usize) -> Arena<T>

Creates a new empty Arena with enough capacity to store n nodes.

Source

pub fn capacity(&self) -> usize

Returns the number of nodes the arena can hold without reallocating.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for additional more nodes to be inserted.

The arena may reserve more space to avoid frequent reallocations.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

Source

pub fn get_node_id(&self, node: &Node<T>) -> Option<NodeId>

Retrieves the NodeId corresponding to a Node in the Arena.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let node = arena.get(foo).unwrap();

let node_id = arena.get_node_id(node).unwrap();
assert_eq!(*arena[node_id].get(), "foo");
Source

pub fn get_node_id_at(&self, index: NonZero<usize>) -> Option<NodeId>

Retrieves the NodeId corresponding to the Node at index in the Arena, if it exists.

Note: We use 1 based indexing, so the first element is at 1 and not 0.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let node = arena.get(foo).unwrap();
let index: NonZeroUsize = foo.into();

let new_foo = arena.get_node_id_at(index).unwrap();
assert_eq!(foo, new_foo);

foo.remove(&mut arena);
let new_foo = arena.get_node_id_at(index);
assert!(new_foo.is_none(), "must be none if the node at the index doesn't exist");
Source

pub fn new_node(&mut self, data: T) -> NodeId

Creates a new node from its associated data.

§Panics

Panics if the arena already has usize::max_value() nodes.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");

assert_eq!(*arena[foo].get(), "foo");
Source

pub fn count(&self) -> usize

Counts the number of nodes in arena and returns it.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let _bar = arena.new_node("bar");
assert_eq!(arena.count(), 2);

foo.remove(&mut arena);
assert_eq!(arena.count(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if arena has no nodes, false otherwise.

§Examples
let mut arena = Arena::new();
assert!(arena.is_empty());

let foo = arena.new_node("foo");
assert!(!arena.is_empty());

foo.remove(&mut arena);
assert!(!arena.is_empty());
Source

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

Returns a reference to the node with the given id if in the arena.

Returns None if not available.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

Note that this does not check whether the given node ID is created by the arena.

let mut arena = Arena::new();
let foo = arena.new_node("foo");
let bar = arena.new_node("bar");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

let mut another_arena = Arena::new();
let _ = another_arena.new_node("Another arena");
assert_eq!(another_arena.get(foo).map(|node| *node.get()), Some("Another arena"));
assert!(another_arena.get(bar).is_none());
Source

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

Returns a mutable reference to the node with the given id if in the arena.

Returns None if not available.

§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

*arena.get_mut(foo).expect("The `foo` node exists").get_mut() = "FOO!";
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("FOO!"));
Source

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

Returns an iterator of all nodes in the arena in storage-order.

Note that this iterator returns also removed elements, which can be tested with the is_removed() method on the node.

§Examples
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let _bar = arena.new_node("bar");

let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
assert_eq!(iter.next().map(|node| *node.get()), Some("bar"));
assert_eq!(iter.next().map(|node| *node.get()), None);
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let bar = arena.new_node("bar");
bar.remove(&mut arena);

let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| (*node.get(), node.is_removed())), Some(("foo", false)));
assert_eq!(iter.next().map_or(false, |node| node.is_removed()), true);
assert_eq!(iter.next().map(|node| (*node.get(), node.is_removed())), None);
Source

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

Returns a mutable iterator of all nodes in the arena in storage-order.

Note that this iterator returns also removed elements, which can be tested with the is_removed() method on the node.

§Example
let arena: &mut Arena<i64> = &mut Arena::new();
let a = arena.new_node(1);
let b = arena.new_node(2);
assert!(a.checked_append(b, arena).is_ok());

for node in arena.iter_mut() {
    let data = node.get_mut();
    *data = data.wrapping_add(4);
}

let node_refs = arena.iter().map(|i| i.get().clone()).collect::<Vec<_>>();
assert_eq!(node_refs, vec![5, 6]);
Source

pub fn clear(&mut self)

Clears all the nodes in the arena, but retains its allocated capacity.

Note that this does not marks all nodes as removed, but completely removes them from the arena storage, thus invalidating all the node ids that were previously created.

Any attempt to call the is_removed() method on the node id will result in panic behavior.

Source

pub fn as_slice(&self) -> &[Node<T>]

Returns a slice of the inner nodes collection.

Note that this does not return root elements, it simply returns a slice into the internal representation of the arena.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Arena<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

impl<T> Default for Arena<T>

Source§

fn default() -> Arena<T>

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

impl<T> Index<NodeId> for Arena<T>

Source§

type Output = Node<T>

The returned type after indexing.
Source§

fn index(&self, node: NodeId) -> &Node<T>

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<NodeId> for Arena<T>

Source§

fn index_mut(&mut self, node: NodeId) -> &mut Node<T>

Performs the mutable indexing (container[index]) operation. Read more
Source§

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

Source§

fn eq(&self, other: &Arena<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

impl<T> StructuralPartialEq for Arena<T>

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> 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> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the concrete type as &dyn Any.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ShardState for T
where T: 'static + Send + Sync + Default,

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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