pub struct Arena<T, A = DefaultArenaBehavior<T>> { /* private fields */ }
Expand description
An arena of objects of type T
.
use id_arena::Arena;
let mut arena = Arena::<&str>::new();
let a = arena.alloc("Albert");
assert_eq!(arena[a], "Albert");
arena[a] = "Alice";
assert_eq!(arena[a], "Alice");
Implementations§
Source§impl<T, A> Arena<T, A>where
A: ArenaBehavior,
impl<T, A> Arena<T, A>where
A: ArenaBehavior,
Sourcepub fn par_iter(&self) -> ParIter<'_, T, A>
pub fn par_iter(&self) -> ParIter<'_, T, A>
Returns an iterator of shared references which can be used to iterate
over this arena in parallel with the rayon
crate.
§Features
This API requires the rayon
feature of this crate to be enabled.
Sourcepub fn par_iter_mut(&mut self) -> ParIterMut<'_, T, A>
pub fn par_iter_mut(&mut self) -> ParIterMut<'_, T, A>
Returns an iterator of mutable references which can be used to iterate
over this arena in parallel with the rayon
crate.
§Features
This API requires the rayon
feature of this crate to be enabled.
Source§impl<T, A> Arena<T, A>where
A: ArenaBehavior,
impl<T, A> Arena<T, A>where
A: ArenaBehavior,
Sourcepub fn new() -> Arena<T, A>
pub fn new() -> Arena<T, A>
Construct a new, empty Arena
.
use id_arena::Arena;
let mut arena = Arena::<usize>::new();
arena.alloc(42);
Sourcepub fn with_capacity(capacity: usize) -> Arena<T, A>
pub fn with_capacity(capacity: usize) -> Arena<T, A>
Construct a new, empty Arena
with capacity for the given number of
elements.
use id_arena::Arena;
let mut arena = Arena::<usize>::with_capacity(100);
for x in 0..100 {
arena.alloc(x * x);
}
Sourcepub fn alloc(&mut self, item: T) -> A::Id
pub fn alloc(&mut self, item: T) -> A::Id
Allocate item
within this arena and return its id.
use id_arena::Arena;
let mut arena = Arena::<usize>::new();
let _id = arena.alloc(42);
§Panics
Panics if the number of elements in the arena overflows a usize
or
Id
’s index storage representation.
Sourcepub fn alloc_with_id(&mut self, f: impl FnOnce(A::Id) -> T) -> A::Id
pub fn alloc_with_id(&mut self, f: impl FnOnce(A::Id) -> T) -> A::Id
Allocate an item with the id that it will be assigned.
This is useful for structures that want to store their id as their own member.
use id_arena::{Arena, Id};
struct Cat {
id: Id<Cat>,
}
let mut arena = Arena::<Cat>::new();
let kitty = arena.alloc_with_id(|id| Cat { id });
assert_eq!(arena[kitty].id, kitty);
Sourcepub fn next_id(&self) -> A::Id
pub fn next_id(&self) -> A::Id
Get the id that will be used for the next item allocated into this arena.
If you are allocating a struct
that wants to have its id as a member
of itself, prefer the less error-prone Arena::alloc_with_id
method.
Sourcepub fn get(&self, id: A::Id) -> Option<&T>
pub fn get(&self, id: A::Id) -> Option<&T>
Get a shared reference to the object associated with the given id
if
it exists.
If there is no object associated with id
(for example, it might
reference an object allocated within a different arena) then return
None
.
use id_arena::Arena;
let mut arena = Arena::<usize>::new();
let id = arena.alloc(42);
assert!(arena.get(id).is_some());
let other_arena = Arena::<usize>::new();
assert!(other_arena.get(id).is_none());
Sourcepub fn get_mut(&mut self, id: A::Id) -> Option<&mut T>
pub fn get_mut(&mut self, id: A::Id) -> Option<&mut T>
Get an exclusive reference to the object associated with the given id
if it exists.
If there is no object associated with id
(for example, it might
reference an object allocated within a different arena) then return
None
.
use id_arena::Arena;
let mut arena = Arena::<usize>::new();
let id = arena.alloc(42);
assert!(arena.get_mut(id).is_some());
let mut other_arena = Arena::<usize>::new();
assert!(other_arena.get_mut(id).is_none());
Sourcepub fn iter(&self) -> Iter<'_, T, A> ⓘ
pub fn iter(&self) -> Iter<'_, T, A> ⓘ
Iterate over this arena’s items and their ids.
use id_arena::Arena;
let mut arena = Arena::<&str>::new();
arena.alloc("hello");
arena.alloc("hi");
arena.alloc("yo");
for (id, s) in arena.iter() {
assert_eq!(arena.get(id).unwrap(), s);
println!("{:?} -> {}", id, s);
}
Trait Implementations§
Source§impl<T, A> Default for Arena<T, A>where
A: ArenaBehavior,
impl<T, A> Default for Arena<T, A>where
A: ArenaBehavior,
Source§impl<T, A> Index<<A as ArenaBehavior>::Id> for Arena<T, A>where
A: ArenaBehavior,
impl<T, A> Index<<A as ArenaBehavior>::Id> for Arena<T, A>where
A: ArenaBehavior,
Source§impl<T, A> IndexMut<<A as ArenaBehavior>::Id> for Arena<T, A>where
A: ArenaBehavior,
impl<T, A> IndexMut<<A as ArenaBehavior>::Id> for Arena<T, A>where
A: ArenaBehavior,
Source§impl<'a, T, A> IntoIterator for &'a Arena<T, A>where
A: ArenaBehavior,
impl<'a, T, A> IntoIterator for &'a Arena<T, A>where
A: ArenaBehavior,
Source§impl<'a, T, A> IntoIterator for &'a mut Arena<T, A>where
A: ArenaBehavior,
impl<'a, T, A> IntoIterator for &'a mut Arena<T, A>where
A: ArenaBehavior,
Source§impl<T, A> IntoIterator for Arena<T, A>where
A: ArenaBehavior,
impl<T, A> IntoIterator for Arena<T, A>where
A: ArenaBehavior,
Source§impl<'data, T, A> IntoParallelIterator for &'data Arena<T, A>
impl<'data, T, A> IntoParallelIterator for &'data Arena<T, A>
Source§impl<'data, T, A> IntoParallelIterator for &'data mut Arena<T, A>
impl<'data, T, A> IntoParallelIterator for &'data mut Arena<T, A>
Source§type Item = (<A as ArenaBehavior>::Id, &'data mut T)
type Item = (<A as ArenaBehavior>::Id, &'data mut T)
Source§type Iter = ParIterMut<'data, T, A>
type Iter = ParIterMut<'data, T, A>
Source§fn into_par_iter(self) -> Self::Iter
fn into_par_iter(self) -> Self::Iter
self
into a parallel iterator. Read moreSource§impl<T, A> IntoParallelIterator for Arena<T, A>
impl<T, A> IntoParallelIterator for Arena<T, A>
Source§type Item = (<A as ArenaBehavior>::Id, T)
type Item = (<A as ArenaBehavior>::Id, T)
Source§type Iter = IntoParIter<T, A>
type Iter = IntoParIter<T, A>
Source§fn into_par_iter(self) -> Self::Iter
fn into_par_iter(self) -> Self::Iter
self
into a parallel iterator. Read moreimpl<T: Eq, A: Eq> Eq for Arena<T, A>
impl<T, A> StructuralPartialEq for Arena<T, A>
Auto Trait Implementations§
impl<T, A> Freeze for Arena<T, A>
impl<T, A> RefUnwindSafe for Arena<T, A>where
T: RefUnwindSafe,
impl<T, A> Send for Arena<T, A>where
T: Send,
impl<T, A> Sync for Arena<T, A>where
T: Sync,
impl<T, A> Unpin for Arena<T, A>where
T: Unpin,
impl<T, A> UnwindSafe for Arena<T, A>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,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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 moreSource§impl<'data, I> IntoParallelRefIterator<'data> for I
impl<'data, I> IntoParallelRefIterator<'data> for I
Source§impl<'data, I> IntoParallelRefMutIterator<'data> for I
impl<'data, I> IntoParallelRefMutIterator<'data> for I
Source§type Iter = <&'data mut I as IntoParallelIterator>::Iter
type Iter = <&'data mut I as IntoParallelIterator>::Iter
Source§type Item = <&'data mut I as IntoParallelIterator>::Item
type Item = <&'data mut I as IntoParallelIterator>::Item
&'data mut T
reference.Source§fn par_iter_mut(
&'data mut self,
) -> <I as IntoParallelRefMutIterator<'data>>::Iter
fn par_iter_mut( &'data mut self, ) -> <I as IntoParallelRefMutIterator<'data>>::Iter
self
. Read more