Skip to main content

Arena

Trait Arena 

Source
pub trait Arena: Allocator {
    // Required methods
    fn alloc<T>(&self, value: T) -> &mut T;
    fn alloc_with<T>(&self, f: impl FnOnce() -> T) -> &mut T;
    fn alloc_str(&self, src: &str) -> &mut str;
    fn alloc_fmt(&self, arguments: Arguments<'_>) -> &mut str;
    fn alloc_slice_copy<T>(&self, src: &[T]) -> &mut [T]
       where T: Copy;
    fn alloc_slice_clone<T>(&self, src: &[T]) -> &mut [T]
       where T: Clone;
    fn alloc_slice_fill_copy<T>(&self, len: usize, value: T) -> &mut [T]
       where T: Copy;
    fn alloc_slice_fill_clone<T>(&self, len: usize, value: &T) -> &mut [T]
       where T: Clone;
    fn alloc_slice_fill_with<T>(
        &self,
        len: usize,
        f: impl FnMut(usize) -> T,
    ) -> &mut [T];
    fn alloc_slice_fill_default<T>(&self, len: usize) -> &mut [T]
       where T: Default;
    fn alloc_slice_fill_iter<T>(
        &self,
        iter: impl IntoIterator<Item = T>,
    ) -> &mut [T];
}
Expand description

Ergonomic, bumpalo-style allocation on top of any Allocator.

Every method returns a reference (or mutable slice) tied to the borrow of the arena, so the allocation escapes the call and lives for the arena’s lifetime. Implemented for every Allocator (including the three arenas and shared references to them) via a blanket impl.

Required Methods§

Source

fn alloc<T>(&self, value: T) -> &mut T

Allocates value and returns a unique reference to it.

Source

fn alloc_with<T>(&self, f: impl FnOnce() -> T) -> &mut T

Allocates the result of f, evaluated directly into the arena slot.

Source

fn alloc_str(&self, src: &str) -> &mut str

Copies src into the arena and returns the copy.

Source

fn alloc_fmt(&self, arguments: Arguments<'_>) -> &mut str

Formats arguments directly into the arena and returns the resulting string.

The arena-equivalent of format!, with no intermediate global allocation:

use mago_allocator::prelude::*;

let arena = LocalArena::new();
let greeting = arena.alloc_fmt(format_args!("{}-{}", "v", 2));
assert_eq!(greeting, "v-2");
Source

fn alloc_slice_copy<T>(&self, src: &[T]) -> &mut [T]
where T: Copy,

Copies a slice of Copy values into the arena.

Source

fn alloc_slice_clone<T>(&self, src: &[T]) -> &mut [T]
where T: Clone,

Clones a slice of Clone values into the arena.

Source

fn alloc_slice_fill_copy<T>(&self, len: usize, value: T) -> &mut [T]
where T: Copy,

Allocates len copies of value.

Source

fn alloc_slice_fill_clone<T>(&self, len: usize, value: &T) -> &mut [T]
where T: Clone,

Allocates len clones of value.

Source

fn alloc_slice_fill_with<T>( &self, len: usize, f: impl FnMut(usize) -> T, ) -> &mut [T]

Allocates len elements, each produced by f(index).

Source

fn alloc_slice_fill_default<T>(&self, len: usize) -> &mut [T]
where T: Default,

Allocates len default-constructed elements.

Source

fn alloc_slice_fill_iter<T>( &self, iter: impl IntoIterator<Item = T>, ) -> &mut [T]

Collects iter into the arena and returns the resulting slice.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<A: Allocator + ?Sized> Arena for A