pub struct SliceAllocGuard<'a, T, A: BasicAlloc + ?Sized> { /* private fields */ }Expand description
A RAII guard for a heap‐allocated slice that tracks how many elements have been initialized.
On drop, it will:
- Run destructors for each initialized element.
- Deallocate the entire slice of memory.
Use init or init_unchecked
to initialize elements one by one, extend_init to
initialize many elements at once, and release to take
ownership of the fully‐initialized slice without running cleanup.
§Examples
# extern crate alloc;
# use core::ptr::NonNull;
# use memapi2::{
# helpers::SliceAllocGuard,
# Alloc,
# DefaultAlloc,
# data::type_props::SizedProps,
# Layout
# };
# let alloc = DefaultAlloc;
# let len = 5;
let mut guard = unsafe { SliceAllocGuard::new(
alloc.alloc(unsafe { Layout::from_size_align_unchecked(u32::SZ * len, u32::ALN) })
.unwrap().cast(),
&alloc,
len
) };
for i in 0..len {
guard.init(i as u32).unwrap();
}
// All elements are now initialized; take ownership:
// (commented out for this example as the pointer won't be used)
// let slice_ptr = guard.release();Implementations§
Source§impl<'a, T, A: BasicAlloc + ?Sized> SliceAllocGuard<'a, T, A>
impl<'a, T, A: BasicAlloc + ?Sized> SliceAllocGuard<'a, T, A>
Sourcepub const unsafe fn new(
ptr: NonNull<T>,
alloc: &'a A,
full: usize,
) -> SliceAllocGuard<'a, T, A>
pub const unsafe fn new( ptr: NonNull<T>, alloc: &'a A, full: usize, ) -> SliceAllocGuard<'a, T, A>
Creates a new slice guard for full elements at ptr in the given allocator.
§Safety
Callers must ensure that ptr was allocated using alloc, has space for full T, and is readable, writable, valid, and aligned.
Sourcepub const unsafe fn new_with_init(
ptr: NonNull<T>,
alloc: &'a A,
init: usize,
full: usize,
) -> SliceAllocGuard<'a, T, A>
pub const unsafe fn new_with_init( ptr: NonNull<T>, alloc: &'a A, init: usize, full: usize, ) -> SliceAllocGuard<'a, T, A>
Creates a new slice guard for full elements at ptr in the given allocator.
§Safety
In addition to the restrictions of SliceAllocGuard::new, callers must ensure that init is the number of existing initialized elements in the slice.
Sourcepub const fn release(self) -> NonNull<[T]>
pub const fn release(self) -> NonNull<[T]>
Release ownership of the slice without deallocating memory, returning a NonNull<T> pointer to the slice.
Sourcepub const fn release_first(self) -> NonNull<T>
pub const fn release_first(self) -> NonNull<T>
Release ownership of the slice without deallocating memory, returning a NonNull<T> pointer to the slice’s first element.
Sourcepub const fn get_init_part(&self) -> NonNull<[T]>
pub const fn get_init_part(&self) -> NonNull<[T]>
Gets a NonNull<[T]> pointer to the initialized elements of the slice.
Sourcepub const fn get_uninit_part(&self) -> NonNull<[T]>
pub const fn get_uninit_part(&self) -> NonNull<[T]>
Gets a NonNull<[T]> pointer to the uninitialized elements of the slice.
Sourcepub const fn init(&mut self, elem: T) -> Result<(), T>
pub const fn init(&mut self, elem: T) -> Result<(), T>
Initializes the next element of the slice with elem.
§Errors
Returns Err(elem) if the slice is at capacity.
Sourcepub const unsafe fn init_unchecked(&mut self, elem: T)
pub const unsafe fn init_unchecked(&mut self, elem: T)
Initializes the next element of the slice with elem.
§Safety
Callers must ensure that the slice is not at capacity. (initialized() < full())
Sourcepub const fn initialized(&self) -> usize
pub const fn initialized(&self) -> usize
Returns how many elements have been initialized.
Sourcepub const fn is_full(&self) -> bool
pub const fn is_full(&self) -> bool
Returns true if every element in the slice has been initialized.
Sourcepub const fn copy_from_slice(&mut self, slice: &[T]) -> Result<(), usize>where
T: Copy,
pub const fn copy_from_slice(&mut self, slice: &[T]) -> Result<(), usize>where
T: Copy,
Copies as many elements from slice as will fit.
On success, all elements are copied and Ok(()) is returned. If slice.len() > remaining_capacity, it copies as many elements as will fit, advances the initialized count to full, and returns Err(excess).
§Errors
Returns Err(excess) if slice.len() > remaining_capacity.
Sourcepub fn clone_from_slice(&mut self, slice: &[T]) -> Result<(), usize>where
T: Clone,
pub fn clone_from_slice(&mut self, slice: &[T]) -> Result<(), usize>where
T: Clone,
Clones as many elements from slice as will fit.
On success, all elements are cloned and Ok(()) is returned. If
slice.len() > remaining_capacity, it clones as many elements as will fit, advances the
initialized count to full, and returns Err(excess).
§Errors
excess if slice.len() > remaining_capacity.
Sourcepub fn extend_init<I: IntoIterator<Item = T>>(
&mut self,
iter: I,
) -> Result<(), I::IntoIter>
pub fn extend_init<I: IntoIterator<Item = T>>( &mut self, iter: I, ) -> Result<(), I::IntoIter>
Initializes the next elements of the slice with the elements from iter.
§Errors
Returns Err((iter, elem)) if the slice is filled before iteration finishes. The
contained iterator will have been partially consumed.
Methods from Deref<Target = NonNull<T>>§
1.25.0 · Sourcepub unsafe fn as_ref<'a>(&self) -> &'a T
pub unsafe fn as_ref<'a>(&self) -> &'a T
Returns a shared reference to the value. If the value may be uninitialized, as_uninit_ref
must be used instead.
For the mutable counterpart see as_mut.
§Safety
When calling this method, you have to ensure that the pointer is convertible to a reference.
§Examples
use std::ptr::NonNull;
let mut x = 0u32;
let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!");
let ref_x = unsafe { ptr.as_ref() };
println!("{ref_x}");