RawAlloc

Struct RawAlloc 

Source
pub struct RawAlloc<A: Allocator = Global> { /* private fields */ }
Expand description

A safe wrapper around a raw allocation with known layout.

§Safety

This type ensures that:

  • The wrapped pointer is always non-null and properly aligned
  • Memory is automatically deallocated when dropped
  • Reallocation maintains proper alignment and size constraints

§Example

use safe_allocator_api::RawAlloc;

// Create a new allocation of 1024 bytes
let layout = Layout::array::<u8>(1024).unwrap();
let mut alloc = RawAlloc::new(layout).expect("allocation failed");

// Write some data
unsafe {
    core::ptr::write(alloc.as_mut_ptr(), 42u8);
}

// Automatically deallocated when dropped

Implementations§

Source§

impl<A: Allocator> RawAlloc<A>

Source

pub fn new_in(layout: Layout, allocator: A) -> Result<Self, AllocError>

Creates a new allocation with the given layout using the provided allocator.

This is equivalent to calling Allocator::allocate but provides automatic cleanup when the allocation is no longer needed.

§Arguments
  • layout - The desired memory layout
  • allocator - The allocator to use
§Errors

Returns AllocError if the allocator reports an error or if the layout has a size of 0.

§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]

use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;

let layout = Layout::new::<u64>();
let alloc = RawAlloc::new_in(layout, Global)?;
Source

pub fn new_zeroed_in(layout: Layout, allocator: A) -> Result<Self, AllocError>

Creates a new zeroed allocation with the given layout using the provided allocator.

This is equivalent to calling Allocator::allocate_zeroed but provides automatic cleanup when the allocation is no longer needed.

§Errors

Returns AllocError if the allocator reports an error or if the layout has a size of 0.

Source

pub fn grow(&mut self, new_layout: Layout) -> Result<(), AllocError>

Attempts to grow the allocation to the new layout.

§Errors

Returns AllocError if:

  • The allocator reports an error
  • The new layout has a size of 0
  • The new size is smaller than the current size (use Self::shrink instead)
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]

use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;

let layout = Layout::array::<u8>(100).unwrap();
let mut alloc = RawAlloc::new(layout)?;

// Grow the allocation
let new_layout = Layout::array::<u8>(200).unwrap();
alloc.grow(new_layout)?;
Source

pub fn grow_zeroed(&mut self, new_layout: Layout) -> Result<(), AllocError>

Attempts to grow the allocation to the new layout, zeroing the additional memory.

This is equivalent to Self::grow but ensures any additional memory is zeroed.

§Errors

Returns AllocError if:

  • The allocator reports an error
  • The new layout has a size of 0
  • The new size is smaller than the current size (use Self::shrink instead)
Source

pub fn shrink(&mut self, new_layout: Layout) -> Result<(), AllocError>

Attempts to shrink the allocation to the new layout.

§Errors

Returns AllocError if:

  • The allocator reports an error
  • The new layout has a size of 0
  • The new size is larger than the current size (use Self::grow instead)
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]

use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;

let layout = Layout::array::<u8>(200).unwrap();
let mut alloc = RawAlloc::new(layout)?;

// Shrink the allocation
let new_layout = Layout::array::<u8>(100).unwrap();
alloc.shrink(new_layout)?;
Source

pub fn as_ptr(&self) -> *const u8

Returns a raw pointer to the allocated memory.

§Safety

The caller must ensure that the memory is accessed according to the original layout constraints.

Source

pub fn as_mut_ptr(&mut self) -> *mut u8

Returns a raw mutable pointer to the allocated memory.

§Safety

The caller must ensure that the memory is accessed according to the original layout constraints.

Source

pub fn as_slice(&self) -> &[u8]

Returns a slice reference to the allocated memory.

This provides a safe interface to access the allocated memory as a byte slice.

§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]

use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;

let layout = Layout::array::<u8>(100).unwrap();
let alloc = RawAlloc::new(layout)?;
let slice = alloc.as_slice();
assert_eq!(slice.len(), 100);
Source

pub fn as_mut_slice(&mut self) -> &mut [u8]

Returns a mutable slice reference to the allocated memory.

This provides a safe interface to access the allocated memory as a mutable byte slice.

§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]

use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;

let layout = Layout::array::<u8>(100).unwrap();
let mut alloc = RawAlloc::new(layout)?;
let slice = alloc.as_mut_slice();
slice[0] = 42;
assert_eq!(slice[0], 42);
Source

pub fn layout(&self) -> Layout

Returns the layout used for this allocation.

Source

pub fn len(&self) -> usize

Represents the length of the allocation.

§Remarks

This is the length with which the allocation was created with, extracted from the Layout; in practice, due to alignment, the number of available bytes may be slightly larger in practice; but you shouldn’t rely on that.

Source

pub fn is_empty(&self) -> bool

Checks if the allocation is empty.

Source§

impl RawAlloc

Source

pub fn new(layout: Layout) -> Result<Self, AllocError>

Creates a new allocation with the given layout using the global allocator.

This is equivalent to calling Self::new_in with the global allocator.

Source

pub fn new_zeroed(layout: Layout) -> Result<Self, AllocError>

Creates a new zeroed allocation with the given layout using the global allocator.

This is equivalent to calling Self::new_zeroed_in with the global allocator.

Trait Implementations§

Source§

impl<A: Allocator> Debug for RawAlloc<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: Allocator> Drop for RawAlloc<A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<A: Allocator> Send for RawAlloc<A>

Source§

impl<A: Allocator> Sync for RawAlloc<A>

Auto Trait Implementations§

§

impl<A> Freeze for RawAlloc<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for RawAlloc<A>
where A: RefUnwindSafe,

§

impl<A> Unpin for RawAlloc<A>
where A: Unpin,

§

impl<A> UnwindSafe for RawAlloc<A>
where A: UnwindSafe,

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.