pub struct ManuallyDropArena<T, Options: ArenaOptions<T> = Options> { /* private fields */ }
Expand description

Like Arena, but returns references of any lifetime, including 'static.

This lets the arena be used without being borrowed, but it comes with the tradeoff that the arena leaks memory unless the unsafe drop method is called.

Implementations§

source§

impl<T, Options: ArenaOptions<T>> ManuallyDropArena<T, Options>

source

pub fn new() -> Self

Creates a new ManuallyDropArena.

source

pub unsafe fn drop(&mut self)

Drops the contents of the arena. The arena will leak memory when dropped unless this method is called.

Safety

You must ensure that no references to items (or parts of items) in the arena exist when calling this method, except possibly for references within the items themselves.

However, if there are references to other items (or parts of items) within the items themselves, at least one of the following must be true:

  • T does not have a custom Drop impl.
  • T’s Drop impl does not directly or indirectly access any data via the references to other items or parts of items. (This is essentially the requirement imposed by #[may_dangle].)

Additionally, there must be no instances of Iter or IterMut for this arena.

source

pub unsafe fn manually_drop(&mut self)

Alias of Self::drop. Can be used to prevent name collisions when this arena is stored in a Deref type:

let mut arena = Box::new(ManuallyDropArena::<u8, 8>::new());
//unsafe { arena.drop() }; // Compile error: resolves to `Drop::drop`
unsafe { arena.manually_drop() }; // Works as expected
Safety

Same requirements as Self::drop.

source

pub fn len(&self) -> usize

Returns the total number of items that have been allocated.

source

pub fn is_empty(&self) -> bool

Checks whether the arena is empty.

source

pub fn alloc<'a>(&mut self, value: T) -> &'a mut Twhere Options: 'a + ArenaOptions<T, Mutable = Bool<true>>,

Allocates a new item in the arena and initializes it with value. Returns a reference to the allocated item. The reference can have any lifetime, including 'static, as long as T outlives that lifetime.

This method calls handle_alloc_error if memory allocation fails; for a version that returns None, see Self::try_alloc.

source

pub fn try_alloc<'a>(&mut self, value: T) -> Option<&'a mut T>where Options: 'a + ArenaOptions<T, Mutable = Bool<true>>,

Like Self::alloc, but returns None if memory allocation fails.

source

pub fn alloc_shared<'a>(&mut self, value: T) -> &'a Twhere Options: 'a,

Allocates a new item in the arena and initializes it with value. Returns a shared/immutable reference to the allocated item. The reference can have any lifetime, including 'static, as long as T outlives that lifetime.

This method calls handle_alloc_error if memory allocation fails; for a version that returns None, see Self::try_alloc.

source

pub fn try_alloc_shared<'a>(&mut self, value: T) -> Option<&'a T>where Options: 'a,

Like Self::alloc_shared, but returns None if memory allocation fails.

source

pub fn iter<'a>(&self) -> Iter<'a, T, Options> where Options: ArenaOptions<T, Mutable = Bool<false>>,

Returns an iterator over the items in this arena.

source

pub unsafe fn iter_unchecked<'a>(&self) -> Iter<'a, T, Options>

Returns an iterator over the items in this arena.

Safety

There must be no mutable references (or references derived from mutable references) to items (or parts of items) in this arena or instances of IterMut for this arena.

source

pub unsafe fn iter_mut_unchecked<'a>(&mut self) -> IterMut<'a, T, Options>

Returns a mutable iterator over the items in this arena.

Safety

There must be no references to items (or parts of items) in this arena or instances of Iter or IterMut for this arena.

source

pub unsafe fn into_iter_unchecked(self) -> IntoIter<T, Options>

Returns an owning iterator over the items in this arena.

Safety

There must be no references to items (or parts of items) in this arena or instances of Iter or IterMut for this arena.

source§

impl<T, Options> ManuallyDropArena<T, Options>where Options: ArenaOptions<T, SupportsPositions = Bool<true>>,

source

pub fn iter_at<'a>(&self, position: &Position) -> Iter<'a, T, Options> where Options: ArenaOptions<T, Mutable = Bool<false>>,

Returns an iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

source

pub unsafe fn iter_at_unchecked<'a>( &self, position: &Position ) -> Iter<'a, T, Options>

Returns an iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

Safety

Same requirements as Self::iter_unchecked.

source

pub unsafe fn iter_mut_at_unchecked<'a>( &mut self, position: &Position ) -> IterMut<'a, T, Options>

Returns a mutable iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

Safety

Same requirements as Self::iter_mut_unchecked.

Trait Implementations§

source§

impl<T, Options: ArenaOptions<T>> Default for ManuallyDropArena<T, Options>

source§

fn default() -> Self

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

impl<'a, T, Options> IntoIterator for &'a ManuallyDropArena<T, Options>where Options: ArenaOptions<T, Mutable = Bool<false>>,

§

type IntoIter = Iter<'a, T, Options>

Which kind of iterator are we turning this into?
§

type Item = &'a T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, Options> Send for ManuallyDropArena<T, Options>where T: Send + Sync, Options: ArenaOptions<T>,

source§

impl<T, Options> Sync for ManuallyDropArena<T, Options>where T: Sync, Options: ArenaOptions<T>,

Auto Trait Implementations§

§

impl<T, Options> RefUnwindSafe for ManuallyDropArena<T, Options>where T: RefUnwindSafe, <<Options as ArenaOptions<T>>::ChunkSize as ChunkSizePriv<T>>::Array: RefUnwindSafe, <<Options as ArenaOptions<T>>::SupportsPositions as SupportsPositionsPriv>::Rc: RefUnwindSafe,

§

impl<T, Options> Unpin for ManuallyDropArena<T, Options>where <<Options as ArenaOptions<T>>::SupportsPositions as SupportsPositionsPriv>::Rc: Unpin,

§

impl<T, Options> UnwindSafe for ManuallyDropArena<T, Options>where T: UnwindSafe + RefUnwindSafe, <<Options as ArenaOptions<T>>::ChunkSize as ChunkSizePriv<T>>::Array: RefUnwindSafe, <<Options as ArenaOptions<T>>::SupportsPositions as SupportsPositionsPriv>::Rc: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.