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>
impl<'a, B: FixedRange> Scope<'a, B>
Sourcepub fn alloc_uninit<T>(&self) -> Result<&mut MaybeUninit<T>, AllocError>
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.
Sourcepub fn allocate(&self, layout: NonZeroLayout) -> Result<&mut [u8], AllocError>
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.
Sourcepub fn alloc<T>(&self, value: T) -> Result<&mut T, AllocError>
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.
Sourcepub fn alloc_slice_copy<T: Copy>(
&self,
src: &[T],
) -> Result<&mut [T], AllocError>
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.
Sourcepub fn alloc_str(&self, s: &str) -> Result<&mut str, AllocError>
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.
Sourcepub fn arena_allocated(&self) -> usize
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.