Skip to content
CryoCryo home
Stdliballoc

box

import std::alloc::box; — in the prelude · source

Types

Box<T, A>

type struct Box<T, A = GlobalAlloc> where A: Allocator {
    ptr:   T*;
    alloc: A;

    static new(value: T) -> Box<T, GlobalAlloc>;
    static new_in(value: T, alloc: A) -> Box<T, A>;
    static try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>;
    static try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>;
    static from_raw(raw: T*) -> Box<T, GlobalAlloc>;
    as_ptr(&this) -> T*;
    get_ref(&this) -> T*;
    get_mut(mut &this) -> T*;
    into_raw(mut this) -> T*;
    leak(mut this) -> T*;
}

A Box owns exactly one heap value plus the allocator instance that produced it. Moves transfer ownership; the final holder's drop runs T::drop() and returns the storage.

mut b: Box<Config> = Box::<Config>::new(config);
b.port = 8080;          // auto-deref through Deref<T>

Note that into_raw and leak transfer both the value and the allocation, so T::drop() does not run on those paths.

Properties

PropertyType
ptrT*
allocA

Constructors

static new(value: T) -> Box<T, GlobalAlloc>;

Allocate heap storage via GlobalAlloc and move value into it. Panics if the allocator refuses the request; use try_new to recover from out-of-memory.

static new_in(value: T, alloc: A) -> Box<T, A>;

Allocate with a caller-supplied allocator. Panics on allocation failure; use try_new_in for fallible construction.

static try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>;

Fallible constructor backed by GlobalAlloc. Returns Err(AllocError) if the allocator refuses the request (out of memory, zero-sized T, etc.).

static try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>;

Fallible constructor with a caller-supplied allocator. The allocator instance is stored in the resulting Box and is used to free the storage on drop.

static from_raw(raw: T*) -> Box<T, GlobalAlloc>;

Re-wrap a pointer previously produced by into_raw (or leak). The pointer must have come from a Box<T, GlobalAlloc>; using this constructor on a pointer that was allocated by a different allocator yields a Box that will free with the wrong allocator on drop. Custom-allocator users that need this round-trip should reconstitute the allocator + pointer themselves and build Box<T, A> { ptr: ..., alloc: ... } directly.

Methods

as_ptr(&this) -> T*;

Raw pointer to the owned value. Valid for the lifetime of the Box; dereferencing after drop is undefined.

get_ref(&this) -> T*;

Borrow the boxed value. Sound for the lifetime of the Box. This is the ergonomic read/write accessor to prefer over as_ptr until language-level auto-deref lands: *b.get_ref() / b.get_ref().field.

get_mut(mut &this) -> T*;

Mutable borrow of the boxed value. A Box is a unique owner, so this is always sound (unlike Rc/Arc, where mutation must be gated on uniqueness).

into_raw(mut this) -> T*;

Give up ownership and return the raw pointer. The caller becomes responsible for freeing the memory (typically via Box::from_raw); dropping this box afterwards would double-free.

leak(mut this) -> T*;

Bridge alias for into_raw, kept so legacy callers reading the new stdlib can still spell "leak this allocation." Same behavior: give up ownership and return the raw pointer.

Trait implementations

implement<T, A> trait Drop for struct Box<T, A> where A: Allocator
implement<T> trait Clone for struct Box<T, GlobalAlloc> where T: Clone
implement<T, A> trait Deref<T> for struct Box<T, A>

Deref<T> is what makes *b, b.field, and b.method() work directly on the box. Clone is a deep copy into fresh GlobalAlloc storage.