Skip to content
CryoCryo home
Stdlibcollections

raw_buffer

import std::collections::raw_buffer; · source

Types

RawBuffer<T, A>

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

    static new() -> RawBuffer<T>;
    static new_in(alloc: A) -> RawBuffer<T, A>;
    ptr(&this) -> T*;
    into_raw(mut this) -> T*;
    capacity(&this) -> u64;
    reserve(mut &this, used: u64, additional: u64, min_capacity: u64) -> Result<(), AllocError>;
    reserve_exact(mut &this, used: u64, additional: u64) -> Result<(), AllocError>;
    shrink_to(mut &this, target: u64) -> void;
    resize(mut &this, new_capacity: u64) -> Result<(), AllocError>;
}

The untyped backing store: a pointer, a capacity, and an allocator, with no notion of how many slots are in use. String keeps its own length beside one of these and passes it in as used when growing. reserve doubles past min_capacity; reserve_exact allocates exactly what is asked. into_raw hands the allocation to the caller and forgets it.

Array deliberately does not use it — RawBuffer puts capacity before length, and Array's layout has to match the [T] fat pointer.

Properties

PropertyType
ptrT*
capacityu64
allocA

Constructors

static new() -> RawBuffer<T>;

Empty buffer backed by GlobalAlloc. No heap storage until the first reserve call.

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

Empty buffer with a caller-supplied allocator. Ownership of alloc transfers to the buffer.

Methods

ptr(&this) -> T*;
into_raw(mut this) -> T*;

Consume the buffer and return its raw pointer, suppressing the deallocating Drop. The caller owns the storage afterward (it is leaked unless later reclaimed). Mirrors Box::into_raw.

capacity(&this) -> u64;
reserve(mut &this, used: u64, additional: u64, min_capacity: u64) -> Result<(), AllocError>;

Ensure at least additional more slots past used fit. Over-allocates along the doubling curve, seeded by min_capacity when the buffer is small (so empty-to-nonempty transitions don't start at a single slot).

reserve_exact(mut &this, used: u64, additional: u64) -> Result<(), AllocError>;

Allocate exactly enough for used + additional. No over- allocation. Useful when the caller already knows the final size (e.g., Array::with_capacity).

shrink_to(mut &this, target: u64) -> void;

Shrink capacity to target, which must be at least used. If shrinking fails, the buffer keeps its current capacity; losing capacity is fine, but losing data is not.

resize(mut &this, new_capacity: u64) -> Result<(), AllocError>;

Reallocate to new_capacity. Assumes new_capacity is the value the caller wants; bounds against used are the caller's problem. A method (not a free function) so the monomorphizer specializes it alongside the rest of RawBuffer<T, A>'s body, mirroring Array::resize_storage.

Trait implementations

implement<T, A> trait Drop for struct RawBuffer<T, A> where A: Allocator

Frees the allocation. Elements are never dropped — the buffer does not know which slots are live — so the owner must drop them first.