id_arena

Struct Arena

Source
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,

Source

pub fn par_iter(&self) -> ParIter<'_, T, A>
where T: Sync, A::Id: Send,

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.

Source

pub fn par_iter_mut(&mut self) -> ParIterMut<'_, T, A>
where T: Send + Sync, A::Id: Send,

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,

Source

pub fn new() -> Arena<T, A>

Construct a new, empty Arena.

use id_arena::Arena;

let mut arena = Arena::<usize>::new();
arena.alloc(42);
Source

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);
}
Source

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.

Source

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);
Source

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.

Source

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());
Source

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());
Source

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);
}
Source

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

Iterate over this arena’s items and their ids, allowing mutation of each item.

Source

pub fn len(&self) -> usize

Get the number of objects allocated in this arena.

use id_arena::Arena;

let mut arena = Arena::<&str>::new();

arena.alloc("hello");
arena.alloc("hi");

assert_eq!(arena.len(), 2);

Trait Implementations§

Source§

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

Source§

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

Returns a copy 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, A: Debug> Debug for Arena<T, A>

Source§

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

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

impl<T, A> Default for Arena<T, A>
where A: ArenaBehavior,

Source§

fn default() -> Arena<T, A>

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

impl<T, A> Index<<A as ArenaBehavior>::Id> for Arena<T, A>
where A: ArenaBehavior,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, id: A::Id) -> &T

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

impl<T, A> IndexMut<<A as ArenaBehavior>::Id> for Arena<T, A>
where A: ArenaBehavior,

Source§

fn index_mut(&mut self, id: A::Id) -> &mut T

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

impl<'a, T, A> IntoIterator for &'a Arena<T, A>
where A: ArenaBehavior,

Source§

type Item = (<A as ArenaBehavior>::Id, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, A>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T, A>

Creates an iterator from a value. Read more
Source§

impl<'a, T, A> IntoIterator for &'a mut Arena<T, A>
where A: ArenaBehavior,

Source§

type Item = (<A as ArenaBehavior>::Id, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, A>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, T, A>

Creates an iterator from a value. Read more
Source§

impl<T, A> IntoIterator for Arena<T, A>
where A: ArenaBehavior,

Source§

type Item = (<A as ArenaBehavior>::Id, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, A>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T, A>

Creates an iterator from a value. Read more
Source§

impl<'data, T, A> IntoParallelIterator for &'data Arena<T, A>
where A: ArenaBehavior, A::Id: Send, T: Sync,

Source§

type Item = (<A as ArenaBehavior>::Id, &'data T)

The type of item that the parallel iterator will produce.
Source§

type Iter = ParIter<'data, T, A>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

impl<'data, T, A> IntoParallelIterator for &'data mut Arena<T, A>
where A: ArenaBehavior, A::Id: Send, T: Send + Sync,

Source§

type Item = (<A as ArenaBehavior>::Id, &'data mut T)

The type of item that the parallel iterator will produce.
Source§

type Iter = ParIterMut<'data, T, A>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

impl<T, A> IntoParallelIterator for Arena<T, A>
where A: ArenaBehavior, A::Id: Send, T: Send,

Source§

type Item = (<A as ArenaBehavior>::Id, T)

The type of item that the parallel iterator will produce.
Source§

type Iter = IntoParIter<T, A>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

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

Source§

fn eq(&self, other: &Arena<T, A>) -> 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, A: Eq> Eq for Arena<T, A>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
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, dst: *mut T)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<'data, I> IntoParallelRefIterator<'data> for I
where I: 'data + ?Sized, &'data I: IntoParallelIterator,

Source§

type Iter = <&'data I as IntoParallelIterator>::Iter

The type of the parallel iterator that will be returned.
Source§

type Item = <&'data I as IntoParallelIterator>::Item

The type of item that the parallel iterator will produce. This will typically be an &'data T reference type.
Source§

fn par_iter(&'data self) -> <I as IntoParallelRefIterator<'data>>::Iter

Converts self into a parallel iterator. Read more
Source§

impl<'data, I> IntoParallelRefMutIterator<'data> for I

Source§

type Iter = <&'data mut I as IntoParallelIterator>::Iter

The type of iterator that will be created.
Source§

type Item = <&'data mut I as IntoParallelIterator>::Item

The type of item that will be produced; this is typically an &'data mut T reference.
Source§

fn par_iter_mut( &'data mut self, ) -> <I as IntoParallelRefMutIterator<'data>>::Iter

Creates the parallel iterator from self. 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.