Struct Arena
pub struct Arena<T> { /* private fields */ }
Expand description
An Arena
structure containing certain Node
s.
Implementations§
§impl<T> Arena<T>
impl<T> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
Creates a new empty Arena
with enough capacity to store n
nodes.
pub fn reserve(&mut self, additional: usize)
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.
pub fn get_node_id(&self, node: &Node<T>) -> Option<NodeId>
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");
pub fn get_node_id_at(&self, index: NonZero<usize>) -> Option<NodeId>
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");
pub fn count(&self) -> usize
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);
pub fn is_empty(&self) -> bool
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());
pub fn get(&self, id: NodeId) -> Option<&Node<T>>
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());
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node<T>>
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!"));
pub fn iter(&self) -> Iter<'_, Node<T>>
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);
pub fn iter_mut(&mut self) -> IterMut<'_, Node<T>>
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]);
pub fn clear(&mut self)
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.
pub fn as_slice(&self) -> &[Node<T>]
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§
impl<T> Eq for Arena<T>where
T: Eq,
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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.