pub struct Arena<T, I = usize, G = usize> { /* private fields */ }
Expand description
The Arena
allows inserting and removing elements that are referred to by
Index
.
See the module-level documentation for example usage and motivation.
Implementations§
Source§impl<T, I: ArenaIndex, G: FixedGenerationalIndex> Arena<T, I, G>
impl<T, I: ArenaIndex, G: FixedGenerationalIndex> Arena<T, I, G>
Sourcepub fn new() -> Arena<T, I, G>
pub fn new() -> Arena<T, I, G>
Constructs a new, empty Arena
.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::<usize>::new();
Sourcepub fn with_capacity(n: usize) -> Arena<T, I, G>
pub fn with_capacity(n: usize) -> Arena<T, I, G>
Constructs a new, empty Arena<T>
with the specified capacity.
The Arena<T>
will be able to hold n
elements without further allocation.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::with_capacity(10);
// These insertions will not require further allocation.
for i in 0..10 {
assert!(arena.try_insert(i).is_ok());
}
// But now we are at capacity, and there is no more room.
assert!(arena.try_insert(99).is_err());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all the items inside the arena, but keep its allocation.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::with_capacity(1);
arena.insert(42);
arena.insert(43);
arena.clear();
assert_eq!(arena.capacity(), 2);
Sourcepub fn try_insert(&mut self, value: T) -> Result<Index<T, I, G>, T>
pub fn try_insert(&mut self, value: T) -> Result<Index<T, I, G>, T>
Attempts to insert value
into the arena using existing capacity.
This method will never allocate new capacity in the arena.
If insertion succeeds, then the value
’s index is returned. If
insertion fails, then Err(value)
is returned to give ownership of
value
back to the caller.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
match arena.try_insert(42) {
Ok(idx) => {
// Insertion succeeded.
assert_eq!(arena[idx], 42);
}
Err(x) => {
// Insertion failed.
assert_eq!(x, 42);
}
};
Sourcepub fn insert(&mut self, value: T) -> Index<T, I, G>
pub fn insert(&mut self, value: T) -> Index<T, I, G>
Insert value
into the arena, allocating more capacity if necessary.
The value
’s associated index in the arena is returned.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx = arena.insert(42);
assert_eq!(arena[idx], 42);
Sourcepub fn contains(&self, i: Index<T, I, G>) -> bool
pub fn contains(&self, i: Index<T, I, G>) -> bool
Is the element at index i
in the arena?
Returns true
if the element at i
is in the arena, false
otherwise.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx = arena.insert(42);
assert!(arena.contains(idx));
arena.remove(idx);
assert!(!arena.contains(idx));
Sourcepub fn get(&self, i: Index<T, I, G>) -> Option<&T>
pub fn get(&self, i: Index<T, I, G>) -> Option<&T>
Get a shared reference to the element at index i
if it is in the
arena.
If the element at index i
is not in the arena, then None
is returned.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx = arena.insert(42);
assert_eq!(arena.get(idx), Some(&42));
arena.remove(idx);
assert!(arena.get(idx).is_none());
Sourcepub fn get_mut(&mut self, i: Index<T, I, G>) -> Option<&mut T>
pub fn get_mut(&mut self, i: Index<T, I, G>) -> Option<&mut T>
Get an exclusive reference to the element at index i
if it is in the
arena.
If the element at index i
is not in the arena, then None
is returned.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx = arena.insert(42);
*arena.get_mut(idx).unwrap() += 1;
assert_eq!(arena.remove(idx), Some(43));
assert!(arena.get_mut(idx).is_none());
Sourcepub fn get2_mut(
&mut self,
i1: Index<T, I, G>,
i2: Index<T, I, G>,
) -> (Option<&mut T>, Option<&mut T>)
pub fn get2_mut( &mut self, i1: Index<T, I, G>, i2: Index<T, I, G>, ) -> (Option<&mut T>, Option<&mut T>)
Get a pair of exclusive references to the elements at index i1
and i2
if it is in the
arena.
If the element at index i1
or i2
is not in the arena, then None
is returned for this
element.
§Panics
Panics if i1
and i2
are pointing to the same item of the arena.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx1 = arena.insert(0);
let idx2 = arena.insert(1);
{
let (item1, item2) = arena.get2_mut(idx1, idx2);
*item1.unwrap() = 3;
*item2.unwrap() = 4;
}
assert_eq!(arena[idx1], 3);
assert_eq!(arena[idx2], 4);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of this arena.
The length is the number of elements the arena holds.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
assert_eq!(arena.len(), 0);
let idx = arena.insert(42);
assert_eq!(arena.len(), 1);
let _ = arena.insert(0);
assert_eq!(arena.len(), 2);
assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.len(), 1);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the arena contains no elements
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
assert!(arena.is_empty());
let idx = arena.insert(42);
assert!(!arena.is_empty());
assert_eq!(arena.remove(idx), Some(42));
assert!(arena.is_empty());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Get the capacity of this arena.
The capacity is the maximum number of elements the arena can hold without further allocation, including however many it currently contains.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::with_capacity(10);
assert_eq!(arena.capacity(), 10);
// `try_insert` does not allocate new capacity.
for i in 0..10 {
assert!(arena.try_insert(1).is_ok());
assert_eq!(arena.capacity(), 10);
}
// But `insert` will if the arena is already at capacity.
arena.insert(0);
assert!(arena.capacity() > 10);
Sourcepub fn iter(&self) -> Iter<'_, T, I, G> ⓘ
pub fn iter(&self) -> Iter<'_, T, I, G> ⓘ
Iterate over shared references to the elements in this arena.
Yields pairs of (Index<T>, &T)
items.
Order of iteration is not defined.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
for i in 0..10 {
arena.insert(i * i);
}
for (idx, value) in arena.iter() {
println!("{} is at index {:?}", value, idx);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, I, G> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T, I, G> ⓘ
Iterate over exclusive references to the elements in this arena.
Yields pairs of (Index<T>, &mut T)
items.
Order of iteration is not defined.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
for i in 0..10 {
arena.insert(i * i);
}
for (_idx, value) in arena.iter_mut() {
*value += 5;
}
Sourcepub fn drain(&mut self) -> Drain<'_, T, I, G> ⓘ
pub fn drain(&mut self) -> Drain<'_, T, I, G> ⓘ
Iterate over elements of the arena and remove them.
Yields pairs of (Index<T>, T)
items.
Order of iteration is not defined.
Note: All elements are removed even if the iterator is only partially consumed or not consumed at all.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx_1 = arena.insert("hello");
let idx_2 = arena.insert("world");
assert!(arena.get(idx_1).is_some());
assert!(arena.get(idx_2).is_some());
for (idx, value) in arena.drain() {
assert!((idx == idx_1 && value == "hello") || (idx == idx_2 && value == "world"));
}
assert!(arena.get(idx_1).is_none());
assert!(arena.get(idx_2).is_none());
Source§impl<T, I: ArenaIndex, G: GenerationalIndex> Arena<T, I, G>
impl<T, I: ArenaIndex, G: GenerationalIndex> Arena<T, I, G>
Sourcepub fn remove(&mut self, i: Index<T, I, G>) -> Option<T>
pub fn remove(&mut self, i: Index<T, I, G>) -> Option<T>
Remove the element at index i
from the arena.
If the element at index i
is still in the arena, then it is
returned. If it is not in the arena, then None
is returned.
§Examples
use typed_generational_arena::StandardArena;
let mut arena = StandardArena::new();
let idx = arena.insert(42);
assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.remove(idx), None);
Sourcepub fn retain(&mut self, predicate: impl FnMut(Index<T, I, G>, &T) -> bool)
pub fn retain(&mut self, predicate: impl FnMut(Index<T, I, G>, &T) -> bool)
Retains only the elements specified by the predicate.
In other words, remove all indices such that predicate(index, &value)
returns false
.
§Examples
use typed_generational_arena::StandardArena;
let mut crew = StandardArena::new();
crew.extend(&["Jim Hawkins", "John Silver", "Alexander Smollett", "Israel Hands"]);
let pirates = ["John Silver", "Israel Hands"]; // too dangerous to keep them around
crew.retain(|_index, member| !pirates.contains(member));
let mut crew_members = crew.iter().map(|(_, member)| **member);
assert_eq!(crew_members.next(), Some("Jim Hawkins"));
assert_eq!(crew_members.next(), Some("Alexander Smollett"));
assert!(crew_members.next().is_none());
Trait Implementations§
Source§impl<T, Idx: ArenaIndex, G: FixedGenerationalIndex> Extend<T> for Arena<T, Idx, G>
impl<T, Idx: ArenaIndex, G: FixedGenerationalIndex> Extend<T> for Arena<T, Idx, G>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)