AlignedBox

Struct AlignedBox 

Source
pub struct AlignedBox<T: ?Sized> { /* private fields */ }
Expand description

A wrapper around alloc::boxed::Box which allows allocating aligned heap memory. An instance of AlignedBox<T> consists of a Box<T> and the alloc::alloc::Layout that has been used to allocate the referenced memory.

Implementations§

Source§

impl<T: ?Sized> AlignedBox<T>

Source

pub fn into_raw_parts(from: AlignedBox<T>) -> (*mut T, Layout)

Decompose the AlignedBox into a raw pointer and the layout used during allocation. The caller of this function becomes responsible for proper deallocation of the memory behind the pointer. This can for example be done by reconstructing the AlignedBox using AlignedBox::from_raw_parts.

Source

pub unsafe fn from_raw_parts(ptr: *mut T, layout: Layout) -> AlignedBox<T>

Construct an AlignedBox from a raw pointer and the layout that has been used to allocate the memory behind that pointer. After calling this function, the pointer is owned by the AlignedBox. In particular, the memory will be freed when the AlignedBox is dropped. This is only safe if the given layout is the same as the one that was used during memory allocation.

§Safety

The function is unsafe because improper use can lead to issues, such as double-free. Also, behavior is undefined if the given layout does not correspond to the one used for allocation.

Source§

impl<T> AlignedBox<T>

Source

pub fn new(alignment: usize, value: T) -> Result<AlignedBox<T>, AlignedBoxError>

Store value of type T on the heap, making sure that it is aligned to a multiple of alignment. It is also checked if alignment is a valid alignment for type T or increased to a valid alignment otherwise.

§Example

Place value 17 of type i32 on the heap, aligned to 64 bytes:

use aligned_box::AlignedBox;

let b = AlignedBox::<i32>::new(64, 17);
Source§

impl<T: Default> AlignedBox<[T]>

Source

pub fn slice_from_default( alignment: usize, nelems: usize, ) -> Result<AlignedBox<[T]>, AlignedBoxError>

Allocate memory for nelems values of type T on the heap, making sure that it is aligned to a multiple of alignment. All values are initialized by the default value of type T. It is also checked if alignment is a valid alignment for type T or increased to a valid alignment otherwise.

§Example

Allocate memory for 1024 values of type f32 on the heap, aligned to 128 bytes. Values are initialized by their default value:

use aligned_box::AlignedBox;

let b = AlignedBox::<[f32]>::slice_from_default(128, 1024);
Source

pub fn realloc_with_default( &mut self, nelems: usize, ) -> Result<(), AlignedBoxError>

Resize allocated memory to fit nelem values of type T. The original alignment requested when creating the AlignedBox will still be obeyed. If nelem is larger than the current amount of stored elements, the newly allocated elements will be initialized with the default value of type T. If nelem is smaller than the current amount of stored elements, any excess elements will be dropped. In case realloc fails, those dropped elements will be reinitialized with the default value of type T.

§Example

Create an AlignedBox::<f32> with 1024 elements. Extend to 2048 elements. Initialize all new elements by the default value of f32, i.e., 0.0.

use aligned_box::AlignedBox;

let mut b = AlignedBox::<[f32]>::slice_from_default(128, 1024).unwrap();
b.realloc_with_default(2048);
Source§

impl<T: Copy> AlignedBox<[T]>

Source

pub fn slice_from_value( alignment: usize, nelems: usize, value: T, ) -> Result<AlignedBox<[T]>, AlignedBoxError>

Allocate memory for nelems values of type T on the heap, making sure that it is aligned to a multiple of alignment. All values are initialized by copies of value. It is also checked if alignment is a valid alignment for type T or increased to a valid alignment otherwise.

§Example

Allocate memory for 1024 values of type f32 on the heap, aligned to 128 bytes. All values are initialized with PI:

use aligned_box::AlignedBox;

let b = AlignedBox::<[f32]>::slice_from_value(128, 1024, std::f32::consts::PI);
Source

pub fn realloc_with_value( &mut self, nelems: usize, value: T, ) -> Result<(), AlignedBoxError>

Resize allocated memory to fit nelem values of type T. The original alignment requested when creating the AlignedBox will still be obeyed. If nelem is larger than the current amount of stored elements, the newly allocated elements will be initialized with the value given as argument to this function. If nelem is smaller than the current amount of stored elements, any excess elements will be dropped. In case realloc fails, those dropped elements will be reinitialized with the value given to this function.

§Example

Create an AlignedBox::<f32> with 1024 elements. Extend to 2048 elements. Initialize all new elements by 3.14.

use aligned_box::AlignedBox;

let mut b = AlignedBox::<[f32]>::slice_from_default(128, 1024).unwrap();
b.realloc_with_value(2048, 3.14);

Trait Implementations§

Source§

impl<T: Clone + ?Sized> Clone for AlignedBox<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> Deref for AlignedBox<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: ?Sized> DerefMut for AlignedBox<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T: ?Sized> Drop for AlignedBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for AlignedBox<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for AlignedBox<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for AlignedBox<T>
where T: Send + ?Sized,

§

impl<T> Sync for AlignedBox<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for AlignedBox<T>
where T: ?Sized,

§

impl<T> UnwindSafe for AlignedBox<T>
where T: UnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.