pub struct Arena { /* private fields */ }Expand description
The memory that print items, and the printer’s state when printing them, are allocated in.
Everything allocated in an arena lives as long as the arena is borrowed, so the compiler ensures print items aren’t used after the arena is dropped. Since the arena is created by the caller, the print items can also borrow data that outlives it, such as the text of the file being formatted.
let arena = Arena::new();
let print_items = generate(&arena, &file);
let text = print(print_items, options);An arena should be created for each file that’s formatted, and arenas should be dropped in the reverse order they’re created in (ex. when formatting within a format).
The memory of an arena is freed when it’s dropped. To reuse it for the next file
instead, create the arenas from an ArenaPool.
Print items can’t be used after their arena is dropped:
use dprint_core::formatting::Arena;
use dprint_core::formatting::PrintItems;
let print_items = {
let arena = Arena::new();
PrintItems::new(&arena)
};Implementations§
Source§impl Arena
impl Arena
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an arena whose memory is freed when it’s dropped.
This also starts the identifiers of the infos and conditions created while it’s alive from zero, which keeps the printer’s storage of them small.
Sourcepub fn bump(&self) -> &Bump
pub fn bump(&self) -> &Bump
Gets the bump allocator, which can be used to allocate other values that should live as long as the print items.
Note that values allocated in it don’t have their destructors run.
Sourcepub fn alloc_resolver<'a>(
&'a self,
resolver: impl Fn(&mut ConditionResolverContext<'_, '_>) -> Option<bool> + Copy + 'a,
) -> ConditionResolver<'a>
pub fn alloc_resolver<'a>( &'a self, resolver: impl Fn(&mut ConditionResolverContext<'_, '_>) -> Option<bool> + Copy + 'a, ) -> ConditionResolver<'a>
Allocates a condition resolver in the arena.
The resolver needs to be Copy because the arena doesn’t run the destructors of
what’s allocated in it. For a resolver that needs state, allocate the state in the
arena and capture a reference to it (ex. arena.bump().alloc(Cell::new(false))),
using bumpalo’s collections for state that allocates.
use dprint_core::formatting::Arena;
let arena = Arena::new();
let text = String::from("text");
// a `String` isn't `Copy`, so this would never be dropped
arena.alloc_resolver(move |_| Some(text.is_empty()));