SmallArena

Struct SmallArena 

Source
pub struct SmallArena<'tag, T> { /* private fields */ }
Expand description

A “Small” arena based on a resizable slice (i.e. a Vec) that can be indexed with 32-bit Idx32s. This can help reduce memory overhead when using many pointer-heavy objects on 64-bit systems.

You can obtain an instance of this type by calling mk_arena.

Implementations§

Source§

impl<'tag, T> SmallArena<'tag, T>

Source

pub unsafe fn new(tag: InvariantLifetime<'tag>, capacity: usize) -> Self

create a new SmallArena. Don’t do this manually. Use the in_arena macro instead.

§Safety

The whole tagged indexing trick relies on the 'tag you give to this constructor. You must never use this value in another arena, lest you might be able to mix up the indices of the two, which could lead to out of bounds access and thus Undefined Behavior!

Source

pub unsafe fn from_vec(tag: InvariantLifetime<'tag>, data: Vec<T>) -> Self

move a Vec into a SmallArena. This is unlikely to be useful to you, it’s an implementation detail of the recycle_arena! macro.

Source

pub fn into_inner(self) -> Vec<T>

consume the arena and get the data out

Source

pub fn try_add(&mut self, item: T) -> Result<Idx32<'tag>, CapacityExceeded<T>>

Add an item to the arena, get an index or CapacityExceeded back.

Source

pub fn add(&mut self, item: T) -> Idx32<'tag>

Add an item to the arena, get an index back.

Source

pub fn as_slice(&self) -> &[T]

View the current elements as a slice.

§Examples
mk_arena!(arena);
let _ = arena.add("Hi!");
let _ = arena.add("Bye!");
assert_eq!(&["Hi!", "Bye!"], arena.as_slice());
Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Mutate the current elements as a slice.

§Examples
mk_arena!(arena);
let one = arena.add(1u32);
let two = arena.add(2);
arena.as_mut_slice().into_iter().for_each(|i| *i += 1);
assert_eq!(arena[one], 2);
assert_eq!(arena[two], 3);

Trait Implementations§

Source§

impl<'tag, T> Index<Idx<'tag, u32>> for SmallArena<'tag, T>

Available on crate feature alloc only.
Source§

fn index(&self, i: Idx32<'tag>) -> &T

Gets an immutable reference to the value at this index.

Source§

type Output = T

The returned type after indexing.
Source§

impl<'tag, T> IndexMut<Idx<'tag, u32>> for SmallArena<'tag, T>

Available on crate feature alloc only.
Source§

fn index_mut(&mut self, i: Idx32<'tag>) -> &mut T

Gets a mutable reference to the value at this index.

Auto Trait Implementations§

§

impl<'tag, T> Freeze for SmallArena<'tag, T>

§

impl<'tag, T> RefUnwindSafe for SmallArena<'tag, T>
where T: RefUnwindSafe,

§

impl<'tag, T> Send for SmallArena<'tag, T>
where T: Send,

§

impl<'tag, T> Sync for SmallArena<'tag, T>
where T: Sync,

§

impl<'tag, T> Unpin for SmallArena<'tag, T>
where T: Unpin,

§

impl<'tag, T> UnwindSafe for SmallArena<'tag, T>
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> 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, 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.