Skip to main content

Scope

Struct Scope 

Source
pub struct Scope<'a, B: FixedRange> { /* private fields */ }
Expand description

A scratch scope over a BumpArena, created by BumpArena::scope.

Allocate through it; when it drops (normally or on a panic unwind) the arena’s cursor rewinds to where the scope began, reclaiming everything the scope allocated. References handed out by the scope borrow it (&self), so the borrow checker forbids them from outliving the rewind — no unsafe lifetime branding is needed, and a misuse is a compile error (see the compile_fail example on BumpArena::scope).

Scopes nest: call scope on a Scope to checkpoint again.

While the scope is alive the underlying arena is mutably borrowed, so it cannot be used directly — which is exactly what prevents an outer allocation from landing in the region the scope will reclaim.

Implementations§

Source§

impl<'a, B: FixedRange> Scope<'a, B>

Source

pub fn alloc_uninit<T>(&self) -> Result<&mut MaybeUninit<T>, AllocError>

Typed scratch allocation bound to this scope — see BumpArena::alloc_uninit. The returned reference borrows the scope, so it cannot outlive the rewind. Returns &mut MaybeUninit<T>; write a T before assuming it initialized.

&mut from &self is the bump-allocator idiom (cf. bumpalo::Bump::alloc): each call returns a disjoint fresh region, so the &muts never alias.

Source

pub fn allocate(&self, layout: NonZeroLayout) -> Result<&mut [u8], AllocError>

Raw byte scratch allocation bound to this scope — see Allocator::allocate. The returned slice borrows the scope.

&mut from &self is intentional (see alloc_uninit): each call returns a disjoint fresh region.

Source

pub fn alloc<T>(&self, value: T) -> Result<&mut T, AllocError>

Allocate and initialize one T as scratch, bound to this scope — see BumpArena::alloc. The returned &mut T cannot outlive the rewind.

Source

pub fn alloc_slice_copy<T: Copy>( &self, src: &[T], ) -> Result<&mut [T], AllocError>

Copy a slice into the scope, bound to it — see BumpArena::alloc_slice_copy. The returned &mut [T] cannot outlive the rewind.

Source

pub fn alloc_str(&self, s: &str) -> Result<&mut str, AllocError>

Copy a string into the scope, bound to it — see BumpArena::alloc_str. The returned &mut str cannot outlive the rewind.

Source

pub fn arena_allocated(&self) -> usize

Bytes currently allocated from the underlying arena (the absolute cursor, not relative to this scope’s mark). Useful in assertions/tests.

Source

pub fn scope(&mut self) -> Scope<'_, B>

Open a nested scratch scope inside this one. The inner scope reclaims only what it allocated when it drops; this outer scope is untouched.

Trait Implementations§

Source§

impl<'a, B: FixedRange> Drop for Scope<'a, B>

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<'a, B> !RefUnwindSafe for Scope<'a, B>

§

impl<'a, B> !Sync for Scope<'a, B>

§

impl<'a, B> !UnwindSafe for Scope<'a, B>

§

impl<'a, B> Freeze for Scope<'a, B>

§

impl<'a, B> Send for Scope<'a, B>
where B: Send,

§

impl<'a, B> Unpin for Scope<'a, B>

§

impl<'a, B> UnsafeUnpin for Scope<'a, B>

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

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.