AllocGuard

Struct AllocGuard 

Source
pub struct AllocGuard<'a, T: ?Sized, A: Alloc + ?Sized> { /* private fields */ }
Expand description

A RAII guard that owns a single allocation and ensures it is deallocated unless explicitly released.

AllocGuard wraps a NonNull<T> pointer and an allocator reference &A. When the guard goes out of scope, the underlying memory will be deallocated via the allocator.

To take ownership of the allocation without deallocating, call release, which returns the raw pointer and prevents the guard from running its cleanup.

This should be used in any situation where the initialization of a pointer’s data may panic. (e.g., initializing via a clone or any other user-implemented method)

§Examples

# use core::ptr::NonNull;
# use memapi2::{helpers::AllocGuard, Alloc, DefaultAlloc};
# let alloc = DefaultAlloc;
// Allocate space for one `u32` and wrap it in a guard
let layout = core::alloc::Layout::new::<u32>();
let mut guard = unsafe { AllocGuard::new(alloc.alloc(layout).unwrap().cast::<u32>(), &alloc) };

// Initialize the value
unsafe { guard.as_ptr().write(123) };

// If everything is OK, take ownership and prevent automatic deallocation
// (commented out for this example as the pointer won't be used)
// let raw = guard.release();

Implementations§

Source§

impl<'a, T: ?Sized, A: Alloc + ?Sized> AllocGuard<'a, T, A>

Source

pub const unsafe fn new(ptr: NonNull<T>, alloc: &'a A) -> AllocGuard<'a, T, A>

Creates a new guard from a pointer and a reference to an allocator.

§Safety

Callers must guarantee ptr is a valid, readable, writable pointer allocated using alloc.

Source

pub const fn init(&mut self, elem: T)
where T: Sized,

Initializes the value by writing to the contained pointer.

Source

pub const fn release(self) -> NonNull<T>

Releases ownership of the allocation, preventing deallocation, and returns the raw pointer.

Methods from Deref<Target = NonNull<T>>§

1.25.0 · Source

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}");

Trait Implementations§

Source§

impl<T: ?Sized, A: Alloc + ?Sized> Deref for AllocGuard<'_, T, A>

Source§

type Target = NonNull<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &NonNull<T>

Dereferences the value.
Source§

impl<T: ?Sized, A: Alloc + ?Sized> Drop for AllocGuard<'_, T, A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T, A> Freeze for AllocGuard<'a, T, A>
where T: ?Sized, A: ?Sized,

§

impl<'a, T, A> RefUnwindSafe for AllocGuard<'a, T, A>

§

impl<'a, T, A> !Send for AllocGuard<'a, T, A>

§

impl<'a, T, A> !Sync for AllocGuard<'a, T, A>

§

impl<'a, T, A> Unpin for AllocGuard<'a, T, A>
where T: ?Sized, A: ?Sized,

§

impl<'a, T, A> UnwindSafe for AllocGuard<'a, T, A>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> SizedProps for T

Source§

const SZ: usize = _

The size of the type.
Source§

const ALN: usize = _

The alignment of the type.
Source§

const LAYOUT: Layout = _

The memory layout for the type.
Source§

const IS_ZST: bool = _

Whether the type is zero-sized.
Source§

const MAX_SLICE_LEN: usize = _

The largest safe length for a [Self].
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.