pub struct Box<T: ?Sized, A: Allocator> { /* private fields */ }
Expand description

A replacement for std::boxed::Box that works with custom allocators.

See the module-level documentation for more.

Implementations

Allocates memory in the given allocator then places x into it.

This doesn’t actually allocate if T is zero-sized.

Examples
#![feature(allocator_api)]

use secmem_alloc::boxed::Box;
use std::alloc::System;

let five = Box::new_in(5, System);

Allocates memory in the given allocator then places x into it, returning an error if the allocation fails

This doesn’t actually allocate if T is zero-sized.

Examples
#![feature(allocator_api)]

use secmem_alloc::boxed::Box;
use std::alloc::System;

let five = Box::try_new_in(5, System)?;

Constructs a new box with uninitialized contents in the provided allocator.

Examples
#![feature(allocator_api)]

use secmem_alloc::boxed::Box;
use std::alloc::System;

let mut five = Box::<u32, _>::new_uninit_in(System);

let five = unsafe {
    // Deferred initialization:
    five.as_mut_ptr().write(5);

    five.assume_init()
};

assert_eq!(*five, 5)

Constructs a new box with uninitialized contents in the provided allocator, returning an error if the allocation fails

Examples
#![feature(allocator_api)]

use secmem_alloc::boxed::Box;
use std::alloc::System;

let mut five = Box::<u32, _>::try_new_uninit_in(System)?;

let five = unsafe {
    // Deferred initialization:
    five.as_mut_ptr().write(5);

    five.assume_init()
};

assert_eq!(*five, 5);

Converts to Box<T, A>.

Safety

As with MaybeUninit::assume_init, it is up to the caller to guarantee that the value really is in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.

Examples
#![feature(allocator_api)]

use secmem_alloc::boxed::Box;
use std::alloc::System;

let mut five = Box::<u32, _>::new_uninit_in(System);

let five: Box<u32, _> = unsafe {
    // Deferred initialization:
    five.as_mut_ptr().write(5);

    five.assume_init()
};

assert_eq!(*five, 5)

Trait Implementations

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.