pub type Arena<'a, A = ()> = Arena<Doc<'a, RefDoc<'a, A>, A>>;
Expand description
An arena which can be used to allocate Doc
values.
Aliased Type§
struct Arena<'a, A = ()> { /* private fields */ }
Implementations
§impl<T> Arena<T>
impl<T> Arena<T>
pub fn new() -> Arena<T>
pub fn new() -> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
Construct a new arena with capacity for n
values pre-allocated.
§Example
use typed_arena::Arena;
let arena = Arena::with_capacity(1337);
pub fn alloc(&self, value: T) -> &mut T
pub fn alloc(&self, value: T) -> &mut T
Allocates a value in the arena, and returns a mutable reference to that value.
§Example
use typed_arena::Arena;
let arena = Arena::new();
let x = arena.alloc(42);
assert_eq!(*x, 42);
pub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]where
I: IntoIterator<Item = T>,
pub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]where
I: IntoIterator<Item = T>,
Uses the contents of an iterator to allocate values in the arena. Returns a mutable slice that contains these values.
§Example
use typed_arena::Arena;
let arena = Arena::new();
let abc = arena.alloc_extend("abcdefg".chars().take(3));
assert_eq!(abc, ['a', 'b', 'c']);
pub unsafe fn alloc_uninitialized(&self, num: usize) -> *mut [T]
pub unsafe fn alloc_uninitialized(&self, num: usize) -> *mut [T]
Allocates space for a given number of values, but doesn’t initialize it.
§Unsafety and Undefined Behavior
The same caveats that apply to
std::mem::uninitialized
apply here:
This is incredibly dangerous and should not be done lightly. Deeply consider initializing your memory with a default value instead.
In particular, it is easy to trigger undefined behavior by allocating
uninitialized values, failing to properly initialize them, and then the
Arena
will attempt to drop them when it is dropped. Initializing an
uninitialized value is trickier than it might seem: a normal assignment
to a field will attempt to drop the old, uninitialized value, which
almost certainly also triggers undefined behavior. You must also
consider all the places where your code might “unexpectedly” drop values
earlier than it “should” because of unwinding during panics.
pub fn uninitialized_array(&self) -> *mut [T]
pub fn uninitialized_array(&self) -> *mut [T]
Returns unused space.
This unused space is still not considered “allocated”. Therefore, it
won’t be dropped unless there are further calls to alloc
,
alloc_uninitialized
, or alloc_extend
which is why the method is
safe.
pub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Convert this Arena
into a Vec<T>
.
Items in the resulting Vec<T>
appear in the order that they were
allocated in.
§Example
use typed_arena::Arena;
let arena = Arena::new();
arena.alloc("a");
arena.alloc("b");
arena.alloc("c");
let easy_as_123 = arena.into_vec();
assert_eq!(easy_as_123, vec!["a", "b", "c"]);
Trait Implementations§
Source§impl<'a, A> DocAllocator<'a, A> for Arena<'a, A>
impl<'a, A> DocAllocator<'a, A> for Arena<'a, A>
type Doc = RefDoc<'a, A>
fn alloc(&'a self, doc: Doc<'a, Self::Doc, A>) -> Self::Doc
Source§fn nil(&'a self) -> DocBuilder<'a, Self, A>
fn nil(&'a self) -> DocBuilder<'a, Self, A>
Source§fn newline(&'a self) -> DocBuilder<'a, Self, A>
fn newline(&'a self) -> DocBuilder<'a, Self, A>
Source§fn space(&'a self) -> DocBuilder<'a, Self, A>
fn space(&'a self) -> DocBuilder<'a, Self, A>
Source§fn as_string<U: ToString>(&'a self, data: U) -> DocBuilder<'a, Self, A>
fn as_string<U: ToString>(&'a self, data: U) -> DocBuilder<'a, Self, A>
t.to_string()
. Read moreSource§fn text<U: Into<Cow<'a, str>>>(&'a self, data: U) -> DocBuilder<'a, Self, A>
fn text<U: Into<Cow<'a, str>>>(&'a self, data: U) -> DocBuilder<'a, Self, A>
Source§fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
Source§fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A>
fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A>
S
between the given documents
[A, B, C, ..., Z]
, yielding [A, S, B, S, C, S, ..., S, Z]
. Read more