pub struct ArenaUnique<T> { /* private fields */ }
Expand description

An arena-based unique ownership container allowing allocation and deallocation of objects with exclusive ownership semantics.

ArenaUnique provides exclusive ownership semantics similar to a Box. It utilizes a RawArena for allocation and deallocation of objects, maintaining the sole ownership of the allocated data and enabling safe cleanup when the ArenaUnique instance is dropped.

This container guarantees exclusive access to the allocated data within the arena, allowing single-threaded operations while efficiently managing memory and ensuring cleanup on drop.

Implementations§

source§

impl<T> ArenaUnique<T>

source

pub const fn overhead() -> usize

Returns the constant overhead per allocation to assist with making allocations page-aligned.

source

pub const fn allocation_size() -> usize

Returns the size of each allocation.

source

pub fn with_capacity(capacity: usize) -> Self

source

pub fn allocate(&self, data: T) -> ArenaBox<T>

Allocates a new data instance of type T within the arena, encapsulating it within an ArenaBox.

This method creates a new instance of type T within the RawArena. The provided data is initialized within the arena, and an ArenaBox is returned to manage this allocated data. The ArenaBox serves as a reference to the allocated data within the arena, providing safe access and management of the stored value.

§Safety

The provided data is allocated within the arena and managed by the ArenaBox. Improper handling or misuse of the returned ArenaBox pointer may lead to memory leaks or memory unsafety.

§Example

// Define a struct that will be allocated within the arena
struct MyStruct {
  data: usize,
}

// Create a new instance of ArenaUnique with a specified base capacity
let arena: ArenaUnique<MyStruct> = ArenaUnique::with_capacity(16);

// Allocate a new MyStruct instance within the arena
let data_instance = MyStruct { data: 42 };
let allocated_box = arena.allocate(data_instance);

// Now, allocated_box can be used as a managed reference to the allocated data
assert_eq!(allocated_box.data, 42); // Validate the data stored in the allocated box
source

pub unsafe fn reserve_space(&self) -> Option<ArenaUniqueReservation<T>>

Attempt to reserve space in this arena.

§Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

source

pub unsafe fn forget_reservation(&self, reservation: ArenaUniqueReservation<T>)

Forget a reservation.

§Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

source

pub unsafe fn complete_reservation( &self, reservation: ArenaUniqueReservation<T>, data: T ) -> ArenaBox<T>

Complete a reservation.

§Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

Trait Implementations§

source§

impl<T> Drop for ArenaUnique<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ArenaUnique<T>

§

impl<T> !Send for ArenaUnique<T>

§

impl<T> !Sync for ArenaUnique<T>

§

impl<T> Unpin for ArenaUnique<T>

§

impl<T> !UnwindSafe for ArenaUnique<T>

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>,

§

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>,

§

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.