Skip to main content

AlignedBuf

Struct AlignedBuf 

Source
pub struct AlignedBuf<T> { /* private fields */ }
Expand description

Growable byte buffer with base pointer aligned to max(align_of::<T>(), 8).

Uses u32 for len/cap (max ~4 GiB, matches Pos(u32)).

The buffer base is always at least 8-byte aligned, which guarantees correct alignment for all standard primitive types (up to i64/u64). If the root type T has stricter alignment, the buffer uses that instead.

§Soundness

Allocation: Uses alloc::{alloc, realloc, dealloc} with a layout whose alignment is BUF_ALIGN. New regions are zero-filled. Allocation failure calls handle_alloc_error (never returns null silently).

Provenance exposure: After every grow or clone, the new pointer’s provenance is exposed via expose_provenance. This allows Near::get and session operations to recover the full allocation’s provenance using with_exposed_provenance when following self-relative offsets.

Send/Sync: The buffer is exclusively owned (no aliased mutable pointers). &AlignedBuf only provides &[u8] access. Both impls are sound.

Implementations§

Source§

impl<T> AlignedBuf<T>

Source

pub const fn new() -> Self

Create an empty buffer with a dangling aligned pointer.

Source

pub fn with_capacity(capacity: u32) -> Self

Create an empty buffer pre-allocated to hold at least capacity bytes.

Trait Implementations§

Source§

impl<T> Buf for AlignedBuf<T>

Available on crate feature alloc only.
Source§

const ALIGN: usize = Self::BUF_ALIGN

Buffer alignment guarantee.
Source§

fn empty() -> Self

Create an empty buffer (len = 0).
Source§

fn as_ptr(&self) -> *const u8

Raw pointer to the buffer start.
Source§

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

Mutable raw pointer to the buffer start.
Source§

fn as_bytes(&self) -> &[u8]

View the buffer contents as a byte slice.
Source§

fn len(&self) -> u32

Current byte length.
Source§

fn capacity(&self) -> u32

Current capacity in bytes.
Source§

fn resize(&mut self, new_len: u32, fill: u8)

Grow or shrink the buffer, filling new bytes with fill.
Source§

fn reserve(&mut self, additional: u32)

Ensure at least additional bytes of spare capacity.
Source§

fn extend_from_slice(&mut self, data: &[u8])

Append bytes from a slice.
Source§

fn is_empty(&self) -> bool

Returns true if the buffer contains no bytes.
Source§

fn align_to(&mut self, align: usize)

Pad len up to the next multiple of align.
Source§

fn alloc<U: Flat>(&mut self) -> Pos

Align + allocate space for one U, return its position. Read more
Source§

fn expose_provenance(&self)

Expose provenance so Near::get can recover it via with_exposed_provenance.
Source§

impl<T> Clone for AlignedBuf<T>

Available on crate feature alloc only.
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> Default for AlignedBuf<T>

Available on crate feature alloc only.
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for AlignedBuf<T>

Available on crate feature alloc only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Send for AlignedBuf<T>

Available on crate feature alloc only.
Source§

impl<T> Sync for AlignedBuf<T>

Available on crate feature alloc only.

Auto Trait Implementations§

§

impl<T> Freeze for AlignedBuf<T>

§

impl<T> RefUnwindSafe for AlignedBuf<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for AlignedBuf<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for AlignedBuf<T>

§

impl<T> UnwindSafe for AlignedBuf<T>
where T: 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> 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<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.