Skip to main content

Arena

Struct Arena 

Source
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

Source

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.

Source

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.

Source

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

Trait Implementations§

Source§

impl Default for Arena

Source§

fn default() -> Self

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

impl Drop for Arena

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Freeze for Arena

§

impl !RefUnwindSafe for Arena

§

impl !Sync for Arena

§

impl !UnwindSafe for Arena

§

impl Forget for Arena

§

impl Send for Arena

§

impl Unpin for Arena

§

impl UnsafeUnpin for Arena

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.