#[repr(transparent)]
pub struct JsTypedArray<T: Binary>(_);
Expand description

The family of JS typed array types.

Typed Arrays

JavaScript’s typed arrays are objects that allow efficiently reading and writing raw binary data in memory. In Neon, the generic type JsTypedArray<T> represents a JavaScript typed array with element type T. For example, a JavaScript Uint32Array represents a compact array of 32-bit unsigned integers, and is represented in Neon as a JsTypedArray<u32>.

Neon also offers a set of convenience shorthands for concrete instances of JsTypedArray, named after their corresponding JavaScript type. For example, JsTypedArray<u32> can also be referred to as JsUint32Array.

The following table shows the complete set of typed array types, with both their JavaScript and Neon types:

Rust TypeConvenience TypeJavaScript Type
JsTypedArray<u8>JsUint8ArrayUint8Array
JsTypedArray<i8>JsInt8ArrayInt8Array
JsTypedArray<u16>JsUint16ArrayUint16Array
JsTypedArray<i16>JsInt16ArrayInt16Array
JsTypedArray<u32>JsUint32ArrayUint32Array
JsTypedArray<i32>JsInt32ArrayInt32Array
JsTypedArray<u64>JsBigUint64ArrayBigUint64Array
JsTypedArray<i64>JsBigInt64ArrayBigInt64Array
JsTypedArray<f32>JsFloat32ArrayFloat32Array
JsTypedArray<f64>JsFloat64ArrayFloat64Array

Example: Creating an integer array

This example creates a typed array of unsigned 32-bit integers with a user-specified length:

fn create_int_array(mut cx: FunctionContext) -> JsResult<JsTypedArray<u32>> {
    let len = cx.argument::<JsNumber>(0)?.value(&mut cx) as usize;
    JsTypedArray::new(&mut cx, len)
}

Buffers

Typed arrays are managed with the ArrayBuffer type, which controls the storage of the underlying data buffer, and several typed views for managing access to the buffer. Neon provides access to the ArrayBuffer class with the JsArrayBuffer type.

Node also provides a Buffer type, which is built on top of ArrayBuffer and provides additional functionality. Neon provides access to the Buffer class with the JsBuffer type.

Many of Node’s I/O APIs work with these types, and they can also be used for compact in-memory data structures, which can be shared efficiently between JavaScript and Rust without copying.

Implementations

Constructs an instance from a slice by copying its contents.

This method is defined on JsTypedArray as a convenience and delegates to TypedArray::from_slice.

Constructs a typed array that views buffer.

The resulting typed array has (buffer.size() / size_of::<T>()) elements.

Constructs a typed array for the specified buffer region.

The resulting typed array has region.len() elements and a size of region.size() bytes.

Throws an exception if the region is invalid, for example if the starting offset is not properly aligned, or the length goes beyond the end of the buffer.

Returns information about the backing buffer region for this typed array.

Constructs a new typed array of length len.

The resulting typed array has a newly allocated storage buffer of size (len * size_of::<T>()) bytes.

Returns the JsArrayBuffer that owns the underlying storage buffer for this typed array.

Note that the typed array might only reference a region of the buffer; use the offset() and size() methods to determine the region.

Returns the offset (in bytes) of the typed array from the start of its JsArrayBuffer.

Returns the length of the typed array, i.e. the number of elements.

Note that, depending on the element size, this is not necessarily the same as size(). In particular:

self.size() == self.len() * size_of::<T>()

Trait Implementations

Formats the value using the given formatter. Read more
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Gets a property from a JavaScript object that may be undefined and attempts to downcast the value if it existed. Read more
Gets a property from a JavaScript object as a JsValue. Read more
Gets a property from a JavaScript object and attempts to downcast as a specific type. Equivalent to calling obj.get_value(&mut cx)?.downcast_or_throw(&mut cx). Read more
Available on crate feature napi-6 only.
Statically checked immutable borrow of binary data. Read more
Statically checked mutable borrow of binary data. Read more
Dynamically checked immutable borrow of binary data, returning an error if the the borrow would overlap with a mutable borrow. Read more
Dynamically checked mutable borrow of binary data, returning an error if the the borrow would overlap with an active borrow. Read more
Returns the size, in bytes, of the allocated binary data.
Constructs an instance from a slice by copying its contents.

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.