pub struct Arena<T> { /* private fields */ }
Expand description
Yet another index-based arena.
Implementations§
Source§impl<T> Arena<T>
impl<T> Arena<T>
Sourcepub const fn new() -> Arena<T>
pub const fn new() -> Arena<T>
Creates a new empty arena.
let arena: la_arena::Arena<i32> = la_arena::Arena::new();
assert!(arena.is_empty());
Sourcepub fn with_capacity(capacity: usize) -> Arena<T>
pub fn with_capacity(capacity: usize) -> Arena<T>
Create a new empty arena with specific capacity.
let arena: la_arena::Arena<i32> = la_arena::Arena::with_capacity(42);
assert!(arena.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Empties the arena, removing all contained values.
let mut arena = la_arena::Arena::new();
arena.alloc(1);
arena.alloc(2);
arena.alloc(3);
assert_eq!(arena.len(), 3);
arena.clear();
assert!(arena.is_empty());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the arena.
let mut arena = la_arena::Arena::new();
assert_eq!(arena.len(), 0);
arena.alloc("foo");
assert_eq!(arena.len(), 1);
arena.alloc("bar");
assert_eq!(arena.len(), 2);
arena.alloc("baz");
assert_eq!(arena.len(), 3);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the arena contains no elements.
let mut arena = la_arena::Arena::new();
assert!(arena.is_empty());
arena.alloc(0.5);
assert!(!arena.is_empty());
Sourcepub fn alloc(&mut self, value: T) -> Idx<T>
pub fn alloc(&mut self, value: T) -> Idx<T>
Allocates a new value on the arena, returning the value’s index.
let mut arena = la_arena::Arena::new();
let idx = arena.alloc(50);
assert_eq!(arena[idx], 50);
Sourcepub fn alloc_many<II: IntoIterator<Item = T>>(
&mut self,
iter: II,
) -> IdxRange<T> ⓘ
pub fn alloc_many<II: IntoIterator<Item = T>>( &mut self, iter: II, ) -> IdxRange<T> ⓘ
Densely allocates multiple values, returning the values’ index range.
let mut arena = la_arena::Arena::new();
let range = arena.alloc_many(0..4);
assert_eq!(arena[range], [0, 1, 2, 3]);
Sourcepub fn iter(
&self,
) -> impl ExactSizeIterator<Item = (Idx<T>, &T)> + DoubleEndedIterator + Clone
pub fn iter( &self, ) -> impl ExactSizeIterator<Item = (Idx<T>, &T)> + DoubleEndedIterator + Clone
Returns an iterator over the arena’s elements.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
let idx2 = arena.alloc(40);
let idx3 = arena.alloc(60);
let mut iterator = arena.iter();
assert_eq!(iterator.next(), Some((idx1, &20)));
assert_eq!(iterator.next(), Some((idx2, &40)));
assert_eq!(iterator.next(), Some((idx3, &60)));
Sourcepub fn iter_mut(
&mut self,
) -> impl ExactSizeIterator<Item = (Idx<T>, &mut T)> + DoubleEndedIterator
pub fn iter_mut( &mut self, ) -> impl ExactSizeIterator<Item = (Idx<T>, &mut T)> + DoubleEndedIterator
Returns an iterator over the arena’s mutable elements.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
assert_eq!(arena[idx1], 20);
let mut iterator = arena.iter_mut();
*iterator.next().unwrap().1 = 10;
drop(iterator);
assert_eq!(arena[idx1], 10);
Sourcepub fn values(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator
pub fn values(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator
Returns an iterator over the arena’s values.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
let idx2 = arena.alloc(40);
let idx3 = arena.alloc(60);
let mut iterator = arena.values();
assert_eq!(iterator.next(), Some(&20));
assert_eq!(iterator.next(), Some(&40));
assert_eq!(iterator.next(), Some(&60));
Sourcepub fn values_mut(
&mut self,
) -> impl ExactSizeIterator<Item = &mut T> + DoubleEndedIterator
pub fn values_mut( &mut self, ) -> impl ExactSizeIterator<Item = &mut T> + DoubleEndedIterator
Returns an iterator over the arena’s mutable values.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
assert_eq!(arena[idx1], 20);
let mut iterator = arena.values_mut();
*iterator.next().unwrap() = 10;
drop(iterator);
assert_eq!(arena[idx1], 10);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Reallocates the arena to make it take up as little space as possible.
Trait Implementations§
Source§impl<T> Extend<T> for Arena<T>
impl<T> Extend<T> for Arena<T>
Source§fn extend<II: IntoIterator<Item = T>>(&mut self, iter: II)
fn extend<II: IntoIterator<Item = T>>(&mut self, iter: II)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<T> FromIterator<T> for Arena<T>
impl<T> FromIterator<T> for Arena<T>
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
Source§impl<T> IntoIterator for Arena<T>
impl<T> IntoIterator for Arena<T>
impl<T: Eq> Eq for Arena<T>
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
Mutably borrows from an owned value. Read more