Struct bump_scope::Bump
source · pub struct Bump<A: Allocator + Clone = Global, const MIN_ALIGN: usize = 1, const UP: bool = true>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,{ /* private fields */ }Expand description
A bump allocator.
Every constructor of Bump needs to allocate memory.
§Gotchas
Allocating directly on a Bump is not compatible with entering bump scopes at the same time:
let mut bump: Bump = Bump::new();
let one = bump.alloc(1);
bump.scoped(|bump| {
// whatever
});Instead convert it to a BumpScope first:
let mut bump: Bump = Bump::new();
let bump = bump.as_mut_scope();
let one = bump.alloc(1);
bump.scoped(|bump| {
// whatever
});Implementations§
source§impl<const MIN_ALIGN: usize, const UP: bool> Bump<Global, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
impl<const MIN_ALIGN: usize, const UP: bool> Bump<Global, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
sourcepub fn with_capacity(layout: Layout) -> Self
Available on crate feature alloc only.
pub fn with_capacity(layout: Layout) -> Self
alloc only.Constructs a new, Bump with at least enough space for layout.
§Panics
Panics if the allocation fails.
sourcepub fn try_new() -> Result<Self, AllocError>
Available on crate feature alloc only.
pub fn try_new() -> Result<Self, AllocError>
alloc only.sourcepub fn try_with_size(size: usize) -> Result<Self, AllocError>
Available on crate feature alloc only.
pub fn try_with_size(size: usize) -> Result<Self, AllocError>
alloc only.sourcepub fn try_with_capacity(layout: Layout) -> Result<Self, AllocError>
Available on crate feature alloc only.
pub fn try_with_capacity(layout: Layout) -> Result<Self, AllocError>
alloc only.Constructs a new, Bump with at least enough space for layout.
§Errors
Errors if the allocation fails.
source§impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
sourcepub fn new_in(allocator: A) -> Self
pub fn new_in(allocator: A) -> Self
This is equivalent to Bump::with_capacity_in(512, allocator).
§Panics
Panics if the allocation fails.
sourcepub fn with_size_in(size: usize, allocator: A) -> Self
pub fn with_size_in(size: usize, allocator: A) -> Self
sourcepub fn with_capacity_in(layout: Layout, allocator: A) -> Self
pub fn with_capacity_in(layout: Layout, allocator: A) -> Self
Constructs a new, Bump with at least enough space for layout.
§Panics
Panics if the allocation fails.
sourcepub fn try_new_in(allocator: A) -> Result<Self, AllocError>
pub fn try_new_in(allocator: A) -> Result<Self, AllocError>
This is equivalent to Bump::try_with_capacity_in(512, allocator).
§Errors
Errors if the allocation fails.
sourcepub fn try_with_size_in(size: usize, allocator: A) -> Result<Self, AllocError>
pub fn try_with_size_in(size: usize, allocator: A) -> Result<Self, AllocError>
sourcepub fn try_with_capacity_in(
layout: Layout,
allocator: A
) -> Result<Self, AllocError>
pub fn try_with_capacity_in( layout: Layout, allocator: A ) -> Result<Self, AllocError>
Constructs a new, Bump with at least enough space for layout.
§Errors
Errors if the allocation fails.
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
This will only keep around the newest chunk, which is also the biggest.
sourcepub fn scoped(&mut self, f: impl FnOnce(BumpScope<'_, A, MIN_ALIGN, UP>))
pub fn scoped(&mut self, f: impl FnOnce(BumpScope<'_, A, MIN_ALIGN, UP>))
Calls f with a new child scope.
§Examples
let mut bump: Bump = Bump::new();
bump.scoped(|bump| {
bump.alloc_str("Hello world!");
assert_eq!(bump.stats().allocated(), 12);
});
assert_eq!(bump.stats().allocated(), 0);sourcepub fn scoped_aligned<const NEW_MIN_ALIGN: usize>(
&mut self,
f: impl FnOnce(BumpScope<'_, A, MIN_ALIGN, UP>)
)where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
pub fn scoped_aligned<const NEW_MIN_ALIGN: usize>(
&mut self,
f: impl FnOnce(BumpScope<'_, A, MIN_ALIGN, UP>)
)where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
Calls f with a new child scope of a new minimum alignment.
sourcepub fn scope_guard(&mut self) -> BumpScopeGuardRoot<'_, A, MIN_ALIGN, UP>
pub fn scope_guard(&mut self) -> BumpScopeGuardRoot<'_, A, MIN_ALIGN, UP>
Creates a new BumpScopeGuardRoot.
This allows for creation of child scopes.
§Examples
let mut bump: Bump = Bump::new();
{
let mut guard = bump.scope_guard();
let bump = guard.scope();
bump.alloc_str("Hello world!");
assert_eq!(bump.stats().allocated(), 12);
}
assert_eq!(bump.stats().allocated(), 0);sourcepub fn aligned<const NEW_MIN_ALIGN: usize>(
&mut self,
f: impl FnOnce(BumpScope<'_, A, NEW_MIN_ALIGN, UP>)
)where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
pub fn aligned<const NEW_MIN_ALIGN: usize>(
&mut self,
f: impl FnOnce(BumpScope<'_, A, NEW_MIN_ALIGN, UP>)
)where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
Calls f with this scope but with a new minimum alignment.
sourcepub fn stats(&self) -> Stats<'_, UP>
pub fn stats(&self) -> Stats<'_, UP>
Returns Stats, which provides statistics about the memory usage of the bump allocator.
sourcepub fn checkpoint(&self) -> Checkpoint
pub fn checkpoint(&self) -> Checkpoint
Creates a checkpoint of the current bump position.
§Examples
let checkpoint = bump.checkpoint();
{
let hello = bump.alloc_str("hello");
assert_eq!(bump.stats().allocated(), 5);
}
unsafe { bump.reset_to(checkpoint); }
assert_eq!(bump.stats().allocated(), 0);sourcepub unsafe fn reset_to(&mut self, checkpoint: Checkpoint)
pub unsafe fn reset_to(&mut self, checkpoint: Checkpoint)
Resets the bump position to a previously created checkpoint. The memory that has been allocated since then will be reused by future allocations.
§Safety
- the checkpoint must have been created by this bump allocator
- the bump allocator must not have been
resetsince creation of this checkpoint - there must be no references to allocations made since creation of this checkpoint
sourcepub fn without_dealloc(&self) -> WithoutDealloc<&Self>
pub fn without_dealloc(&self) -> WithoutDealloc<&Self>
Wraps &self in WithoutDealloc so that deallocate becomes a no-op.
sourcepub fn without_shrink(&self) -> WithoutShrink<&Self>
pub fn without_shrink(&self) -> WithoutShrink<&Self>
Wraps &self in WithoutShrink so that shrink becomes a no-op.
sourcepub fn as_mut_scope(&mut self) -> &mut BumpScope<'_, A, MIN_ALIGN, UP>
pub fn as_mut_scope(&mut self) -> &mut BumpScope<'_, A, MIN_ALIGN, UP>
Returns this &mut Bump as a &mut BumpScope.
sourcepub fn into_aligned<const NEW_MIN_ALIGN: usize>(
self
) -> Bump<A, NEW_MIN_ALIGN, UP>where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
pub fn into_aligned<const NEW_MIN_ALIGN: usize>(
self
) -> Bump<A, NEW_MIN_ALIGN, UP>where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
Converts this Bump into a Bump with a new minimum alignment.
sourcepub fn as_aligned_mut<const NEW_MIN_ALIGN: usize>(
&mut self
) -> &mut Bump<A, NEW_MIN_ALIGN, UP>where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
pub fn as_aligned_mut<const NEW_MIN_ALIGN: usize>(
&mut self
) -> &mut Bump<A, NEW_MIN_ALIGN, UP>where
MinimumAlignment<NEW_MIN_ALIGN>: SupportedMinimumAlignment,
Mutably borrows Bump with a new minimum alignment.
This can not decrease the alignment. Trying to decrease alignment will result in a compile error. You can use aligned or scoped_aligned to decrease the alignment.
source§impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
Functions to allocate. Available as fallible or infallible.
sourcepub fn alloc_with<T>(&self, f: impl FnOnce() -> T) -> BumpBox<'_, T> ⓘ
pub fn alloc_with<T>(&self, f: impl FnOnce() -> T) -> BumpBox<'_, T> ⓘ
Pre-allocate space for an object. Once space is allocated f will be called to create the value to be put at that place.
In some situations this can help the compiler realize that T can be constructed at the allocated space instead of having to copy it over.
§Panics
Panics if the allocation fails.
sourcepub fn alloc_default<T: Default>(&self) -> BumpBox<'_, T> ⓘ
pub fn alloc_default<T: Default>(&self) -> BumpBox<'_, T> ⓘ
Allocate an object with its default value.
This is equivalent to alloc_with(T::default()).
§Panics
Panics if the allocation fails.
sourcepub fn alloc_slice_fill<T: Clone>(
&self,
len: usize,
value: T
) -> BumpBox<'_, [T]> ⓘ
pub fn alloc_slice_fill<T: Clone>( &self, len: usize, value: T ) -> BumpBox<'_, [T]> ⓘ
Allocate a slice and fill it with elements by cloning value.
§Panics
Panics if the allocation fails.
sourcepub fn alloc_slice_fill_with<T>(
&self,
len: usize,
f: impl FnMut() -> T
) -> BumpBox<'_, [T]> ⓘ
pub fn alloc_slice_fill_with<T>( &self, len: usize, f: impl FnMut() -> T ) -> BumpBox<'_, [T]> ⓘ
Allocates a slice by fill it with elements returned by calling a closure repeatedly.
This method uses a closure to create new values. If you’d rather
Clone a given value, use (try_)alloc_slice_fill. If you want to use the Default
trait to generate values, you can pass Default::default as the
argument.
§Panics
Panics if the allocation fails.
sourcepub fn alloc_fmt(&self, args: Arguments<'_>) -> BumpBox<'_, str> ⓘ
Available on crate feature alloc only.
pub fn alloc_fmt(&self, args: Arguments<'_>) -> BumpBox<'_, str> ⓘ
alloc only.Allocate a str from format arguments.
For better performance prefer alloc_fmt_mut.
§Panics
Panics if the allocation fails.
Panics if a formatting trait implementation returned an error.
§Examples
let one = 1;
let two = 2;
let string = bump.alloc_fmt(format_args!("{one} + {two} = {}", one + two));
assert_eq!(string, "1 + 2 = 3");sourcepub fn alloc_fmt_mut(&mut self, args: Arguments<'_>) -> BumpBox<'_, str> ⓘ
pub fn alloc_fmt_mut(&mut self, args: Arguments<'_>) -> BumpBox<'_, str> ⓘ
Allocate a str from format arguments.
Unlike alloc_fmt, this function requires a mutable Bump(Scope).
§Panics
Panics if the allocation fails.
Panics if a formatting trait implementation returned an error.
§Examples
let one = 1;
let two = 2;
let string = bump.alloc_fmt_mut(format_args!("{one} + {two} = {}", one + two));
assert_eq!(string, "1 + 2 = 3");sourcepub fn alloc_iter<T>(
&self,
iter: impl IntoIterator<Item = T>
) -> BumpBox<'_, [T]> ⓘ
Available on crate feature alloc only.
pub fn alloc_iter<T>( &self, iter: impl IntoIterator<Item = T> ) -> BumpBox<'_, [T]> ⓘ
alloc only.Allocate elements of an iterator into a slice.
For better performance prefer alloc_iter_exact or alloc_iter_mut(_rev).
§Panics
Panics if the allocation fails.
§Examples
let slice = bump.alloc_iter([1, 2, 3]);
assert_eq!(slice, [1, 2, 3]);sourcepub fn alloc_iter_exact<T, I>(
&self,
iter: impl IntoIterator<Item = T, IntoIter = I>
) -> BumpBox<'_, [T]> ⓘwhere
I: ExactSizeIterator<Item = T>,
pub fn alloc_iter_exact<T, I>(
&self,
iter: impl IntoIterator<Item = T, IntoIter = I>
) -> BumpBox<'_, [T]> ⓘwhere
I: ExactSizeIterator<Item = T>,
sourcepub fn alloc_iter_mut<T>(
&mut self,
iter: impl IntoIterator<Item = T>
) -> BumpBox<'_, [T]> ⓘ
pub fn alloc_iter_mut<T>( &mut self, iter: impl IntoIterator<Item = T> ) -> BumpBox<'_, [T]> ⓘ
Allocate elements of an iterator into a slice.
Unlike alloc_iter, this function requires a mutable Bump(Scope).
When bumping downwards, prefer alloc_iter_mut_rev or alloc_iter_exact as in this case this function incurs an additional copy of the slice internally.
§Panics
Panics if the allocation fails.
§Examples
let slice = bump.alloc_iter_mut([1, 2, 3]);
assert_eq!(slice, [1, 2, 3]);sourcepub fn alloc_iter_mut_rev<T>(
&mut self,
iter: impl IntoIterator<Item = T>
) -> BumpBox<'_, [T]> ⓘ
pub fn alloc_iter_mut_rev<T>( &mut self, iter: impl IntoIterator<Item = T> ) -> BumpBox<'_, [T]> ⓘ
Allocate elements of an iterator into a slice in reverse order.
When bumping upwards, prefer alloc_iter_mut or alloc_iter_exact as in this case this function incurs an additional copy of the slice internally.
§Panics
Panics if the allocation fails.
§Examples
let slice = bump.alloc_iter_mut_rev([1, 2, 3]);
assert_eq!(slice, [3, 2, 1]);sourcepub fn alloc_uninit<T>(&self) -> BumpBox<'_, MaybeUninit<T>> ⓘ
pub fn alloc_uninit<T>(&self) -> BumpBox<'_, MaybeUninit<T>> ⓘ
Allocate an unitialized object.
You can safely initialize the object with init or unsafely with assume_init.
§Panics
Panics if the allocation fails.
§Examples
Safely:
let mut bump: Bump = Bump::new();
let mut five = bump.alloc_uninit();
let five = five.init(5);
assert_eq!(*five, 5)Unsafely:
let mut bump: Bump = Bump::new();
let mut five = bump.alloc_uninit();
let five = unsafe {
five.write(5);
five.assume_init()
};
assert_eq!(*five, 5)sourcepub fn alloc_uninit_slice<T>(&self, len: usize) -> BumpBox<'_, [MaybeUninit<T>]> ⓘ
pub fn alloc_uninit_slice<T>(&self, len: usize) -> BumpBox<'_, [MaybeUninit<T>]> ⓘ
Allocate an unitialized object slice.
You can safely initialize the object with init_fill, init_fill_with, init_copy, init_clone or unsafely with assume_init.
§Panics
Panics if the allocation fails.
§Examples
Safely:
let bump: Bump = Bump::new();
let mut values = bump.alloc_uninit_slice(3);
let values = values.init_copy(&[1, 2, 3]);
assert_eq!(*values, [1, 2, 3])Unsafely:
let bump: Bump = Bump::new();
let mut values = bump.alloc_uninit_slice(3);
let values = unsafe {
values[0].write(1);
values[1].write(2);
values[2].write(3);
values.assume_init()
};
assert_eq!(*values, [1, 2, 3])sourcepub fn alloc_uninit_slice_for<T>(
&self,
slice: &[T]
) -> BumpBox<'_, [MaybeUninit<T>]> ⓘ
pub fn alloc_uninit_slice_for<T>( &self, slice: &[T] ) -> BumpBox<'_, [MaybeUninit<T>]> ⓘ
Allocate an unitialized object slice.
You can safely initialize the object with init_fill, init_fill_with, init_copy, init_clone or unsafely with assume_init.
This is just like (try_)alloc_uninit_slice but uses a slice to provide the len.
This avoids a check for a valid layout. The elements of slice are irrelevant.
§Panics
Panics if the allocation fails.
sourcepub fn alloc_fixed_vec<T>(&self, len: usize) -> FixedBumpVec<'_, T>
pub fn alloc_fixed_vec<T>(&self, len: usize) -> FixedBumpVec<'_, T>
Allocate a FixedBumpVec with the given capacity.
§Panics
Panics if the allocation fails.
§Examples
let mut values = bump.alloc_fixed_vec(3);
values.push(1);
values.push(2);
values.push(3);
assert_eq!(values, [1, 2, 3])sourcepub fn alloc_layout(&self, layout: Layout) -> NonNull<u8>
pub fn alloc_layout(&self, layout: Layout) -> NonNull<u8>
sourcepub fn reserve_bytes(&self, additional: usize)
pub fn reserve_bytes(&self, additional: usize)
Reserves capacity for at least additional more bytes to be bump allocated.
The bump allocator may reserve more space to avoid frequent reallocations.
After calling reserve, chunks().remaining() will be greater than or equal to
additional. Does nothing if capacity is already sufficient.
§Panics
Panics if the allocation fails.
§Examples
let bump: Bump = Bump::new();
assert!(bump.stats().capacity() < 4096);
bump.reserve_bytes(4096);
assert!(bump.stats().capacity() > 4096);sourcepub fn try_alloc<T>(&self, value: T) -> Result<BumpBox<'_, T>, AllocError>
pub fn try_alloc<T>(&self, value: T) -> Result<BumpBox<'_, T>, AllocError>
sourcepub fn try_alloc_with<T>(
&self,
f: impl FnOnce() -> T
) -> Result<BumpBox<'_, T>, AllocError>
pub fn try_alloc_with<T>( &self, f: impl FnOnce() -> T ) -> Result<BumpBox<'_, T>, AllocError>
Pre-allocate space for an object. Once space is allocated f will be called to create the value to be put at that place.
In some situations this can help the compiler realize that T can be constructed at the allocated space instead of having to copy it over.
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_default<T: Default>(
&self
) -> Result<BumpBox<'_, T>, AllocError>
pub fn try_alloc_default<T: Default>( &self ) -> Result<BumpBox<'_, T>, AllocError>
Allocate an object with its default value.
This is equivalent to try_alloc_with(T::default()).
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_slice_copy<T: Copy>(
&self,
slice: &[T]
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_slice_copy<T: Copy>( &self, slice: &[T] ) -> Result<BumpBox<'_, [T]>, AllocError>
sourcepub fn try_alloc_slice_clone<T: Clone>(
&self,
slice: &[T]
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_slice_clone<T: Clone>( &self, slice: &[T] ) -> Result<BumpBox<'_, [T]>, AllocError>
sourcepub fn try_alloc_slice_fill<T: Clone>(
&self,
len: usize,
value: T
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_slice_fill<T: Clone>( &self, len: usize, value: T ) -> Result<BumpBox<'_, [T]>, AllocError>
Allocate a slice and fill it with elements by cloning value.
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_slice_fill_with<T>(
&self,
len: usize,
f: impl FnMut() -> T
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_slice_fill_with<T>( &self, len: usize, f: impl FnMut() -> T ) -> Result<BumpBox<'_, [T]>, AllocError>
Allocates a slice by fill it with elements returned by calling a closure repeatedly.
This method uses a closure to create new values. If you’d rather
Clone a given value, use (try_)alloc_slice_fill. If you want to use the Default
trait to generate values, you can pass Default::default as the
argument.
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_str(&self, src: &str) -> Result<BumpBox<'_, str>, AllocError>
pub fn try_alloc_str(&self, src: &str) -> Result<BumpBox<'_, str>, AllocError>
sourcepub fn try_alloc_fmt(
&self,
args: Arguments<'_>
) -> Result<BumpBox<'_, str>, AllocError>
Available on crate feature alloc only.
pub fn try_alloc_fmt( &self, args: Arguments<'_> ) -> Result<BumpBox<'_, str>, AllocError>
alloc only.Allocate a str from format arguments.
For better performance prefer try_alloc_fmt_mut.
§Errors
Errors if the allocation fails.
Errors if a formatting trait implementation returned an error.
sourcepub fn try_alloc_fmt_mut(
&mut self,
args: Arguments<'_>
) -> Result<BumpBox<'_, str>, AllocError>
pub fn try_alloc_fmt_mut( &mut self, args: Arguments<'_> ) -> Result<BumpBox<'_, str>, AllocError>
Allocate a str from format arguments.
Unlike try_alloc_fmt, this function requires a mutable Bump(Scope).
§Errors
Errors if the allocation fails.
Errors if a formatting trait implementation returned an error.
sourcepub fn try_alloc_iter<T>(
&self,
iter: impl IntoIterator<Item = T>
) -> Result<BumpBox<'_, [T]>, AllocError>
Available on crate feature alloc only.
pub fn try_alloc_iter<T>( &self, iter: impl IntoIterator<Item = T> ) -> Result<BumpBox<'_, [T]>, AllocError>
alloc only.Allocate elements of an iterator into a slice.
For better performance prefer try_alloc_iter_exact or try_alloc_iter_mut(_rev).
§Errors
Errors if the allocation fails.
§Examples
let slice = bump.alloc_iter([1, 2, 3]);
assert_eq!(slice, [1, 2, 3]);sourcepub fn try_alloc_iter_exact<T, I>(
&self,
iter: impl IntoIterator<Item = T, IntoIter = I>
) -> Result<BumpBox<'_, [T]>, AllocError>where
I: ExactSizeIterator<Item = T>,
pub fn try_alloc_iter_exact<T, I>(
&self,
iter: impl IntoIterator<Item = T, IntoIter = I>
) -> Result<BumpBox<'_, [T]>, AllocError>where
I: ExactSizeIterator<Item = T>,
sourcepub fn try_alloc_iter_mut<T>(
&mut self,
iter: impl IntoIterator<Item = T>
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_iter_mut<T>( &mut self, iter: impl IntoIterator<Item = T> ) -> Result<BumpBox<'_, [T]>, AllocError>
Allocate elements of an iterator into a slice.
Unlike try_alloc_iter, this function requires a mutable Bump(Scope).
When bumping downwards, prefer try_alloc_iter_mut_rev or try_alloc_iter_exact as in this case this function incurs an additional copy of the slice internally.
§Errors
Errors if the allocation fails.
§Examples
let slice = bump.alloc_iter_mut([1, 2, 3]);
assert_eq!(slice, [1, 2, 3]);sourcepub fn try_alloc_iter_mut_rev<T>(
&mut self,
iter: impl IntoIterator<Item = T>
) -> Result<BumpBox<'_, [T]>, AllocError>
pub fn try_alloc_iter_mut_rev<T>( &mut self, iter: impl IntoIterator<Item = T> ) -> Result<BumpBox<'_, [T]>, AllocError>
Allocate elements of an iterator into a slice in reverse order.
When bumping upwards, prefer try_alloc_iter_mut or try_alloc_iter_exact as in this case this function incurs an additional copy of the slice internally.
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_uninit<T>(
&self
) -> Result<BumpBox<'_, MaybeUninit<T>>, AllocError>
pub fn try_alloc_uninit<T>( &self ) -> Result<BumpBox<'_, MaybeUninit<T>>, AllocError>
Allocate an unitialized object.
You can safely initialize the object with init or unsafely with assume_init.
§Errors
Errors if the allocation fails.
§Examples
Safely:
let mut bump: Bump = Bump::new();
let mut five = bump.alloc_uninit();
let five = five.init(5);
assert_eq!(*five, 5)Unsafely:
let mut bump: Bump = Bump::new();
let mut five = bump.alloc_uninit();
let five = unsafe {
five.write(5);
five.assume_init()
};
assert_eq!(*five, 5)sourcepub fn try_alloc_uninit_slice<T>(
&self,
len: usize
) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>
pub fn try_alloc_uninit_slice<T>( &self, len: usize ) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>
Allocate an unitialized object slice.
You can safely initialize the object with init_fill, init_fill_with, init_copy, init_clone or unsafely with assume_init.
§Errors
Errors if the allocation fails.
§Examples
Safely:
let bump: Bump = Bump::new();
let mut values = bump.alloc_uninit_slice(3);
let values = values.init_copy(&[1, 2, 3]);
assert_eq!(*values, [1, 2, 3])Unsafely:
let bump: Bump = Bump::new();
let mut values = bump.alloc_uninit_slice(3);
let values = unsafe {
values[0].write(1);
values[1].write(2);
values[2].write(3);
values.assume_init()
};
assert_eq!(*values, [1, 2, 3])sourcepub fn try_alloc_uninit_slice_for<T>(
&self,
slice: &[T]
) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>
pub fn try_alloc_uninit_slice_for<T>( &self, slice: &[T] ) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>
Allocate an unitialized object slice.
You can safely initialize the object with init_fill, init_fill_with, init_copy, init_clone or unsafely with assume_init.
This is just like (try_)alloc_uninit_slice but uses a slice to provide the len.
This avoids a check for a valid layout. The elements of slice are irrelevant.
§Errors
Errors if the allocation fails.
sourcepub fn try_alloc_fixed_vec<T>(
&self,
len: usize
) -> Result<FixedBumpVec<'_, T>, AllocError>
pub fn try_alloc_fixed_vec<T>( &self, len: usize ) -> Result<FixedBumpVec<'_, T>, AllocError>
Allocate a FixedBumpVec with the given capacity.
§Errors
Errors if the allocation fails.
§Examples
let mut values = bump.alloc_fixed_vec(3);
values.push(1);
values.push(2);
values.push(3);
assert_eq!(values, [1, 2, 3])sourcepub fn try_alloc_layout(
&self,
layout: Layout
) -> Result<NonNull<u8>, AllocError>
pub fn try_alloc_layout( &self, layout: Layout ) -> Result<NonNull<u8>, AllocError>
sourcepub fn try_reserve_bytes(&self, additional: usize) -> Result<(), AllocError>
pub fn try_reserve_bytes(&self, additional: usize) -> Result<(), AllocError>
Reserves capacity for at least additional more bytes to be bump allocated.
The bump allocator may reserve more space to avoid frequent reallocations.
After calling reserve, chunks().remaining() will be greater than or equal to
additional. Does nothing if capacity is already sufficient.
§Errors
Errors if the allocation fails.
§Examples
let bump: Bump = Bump::new();
assert!(bump.stats().capacity() < 4096);
bump.reserve_bytes(4096);
assert!(bump.stats().capacity() > 4096);Trait Implementations§
source§impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Allocator for Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
impl<A: Allocator + Clone, const MIN_ALIGN: usize, const UP: bool> Allocator for Bump<A, MIN_ALIGN, UP>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
ptr. Read moresource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout ) -> Result<NonNull<[u8]>, AllocError>
source§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout ) -> Result<NonNull<[u8]>, AllocError>
grow, but also ensures that the new contents are set to zero before being
returned. Read moresource§unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout ) -> Result<NonNull<[u8]>, AllocError>
source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate, but also ensures that the returned memory is zero-initialized. Read more