Skip to main content

BufferStorage

Struct BufferStorage 

Source
pub struct BufferStorage {
    pub bytes: Vec<u8>,
    pub detached: bool,
    pub max_byte_length: Option<usize>,
    pub release: Option<Box<dyn FnOnce()>>,
}
Expand description

The bytes an ArrayBuffer owns, plus whether they have been detached.

transfer() moves the bytes to a new buffer and leaves this one empty and detached; a view of a detached buffer reads undefined from every index.

Fields§

§bytes: Vec<u8>

The bytes, in memory order.

bytes.len() is the byte length: resizing is Vec::resize, which is what keeps the length, the capacity and the pointer from ever disagreeing.

§detached: bool

Whether transfer() has taken the bytes away.

§max_byte_length: Option<usize>

The most bytes this buffer may grow to, or None when it cannot grow at all.

Some(max) is what new ArrayBuffer(n, {maxByteLength: max}) asks for: resizable answers true, maxByteLength answers max, and resize may move the length anywhere up to it. A fixed-length buffer answers false/the byte length.

§release: Option<Box<dyn FnOnce()>>

A host’s “release this block” callback, run when the storage dies.

This is what lets the C API’s JS_NewArrayBuffer honour its free_func contract: the header says the block is released when the buffer is collected, not when it is built, and a host that counts allocations sees the difference. The bytes themselves are copied — this engine’s storage is a Vec<u8> and cannot alias a foreign pointer without a second representation of every typed-array operation — so the callback releases the host’s original block while the storage keeps its own copy. Writes the host makes to its block after the call are therefore not visible to script, which is the one documented difference.

Implementations§

Source§

impl BufferStorage

Source

pub fn fixed(bytes: Vec<u8>) -> BufferStorage

Storage for a fixed-length buffer, which is what every caller but a resizable constructor wants.

Source

pub fn fixed_with_release( bytes: Vec<u8>, release: Box<dyn FnOnce()>, ) -> BufferStorage

Storage for a fixed-length buffer whose origin block has to be released at collection.

The C API’s JS_NewArrayBuffer(ctx, buf, len, free_func, opaque, …): the bytes are a copy, and release is the closure that calls free_func(opaque, buf) when this storage is dropped. Keeping the closure here rather than calling it at construction is the whole difference between honouring the header and approximating it.

Source

pub fn resizable(bytes: Vec<u8>, max: usize) -> BufferStorage

Storage for a resizable buffer of bytes bytes that may grow to max.

Source

pub fn is_resizable(&self) -> bool

Whether resize can change this buffer’s length.

Source

pub fn max_byte_length(&self) -> usize

What maxByteLength reports: the ceiling for a resizable buffer, and the current length for a fixed one — which is what the getter’s IsDetachedBuffer/IsFixedLength split in the spec amounts to once the length stops changing.

Trait Implementations§

Source§

impl Drop for BufferStorage

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.