pub trait TypedArray: Value {
    type Item: Binary;

    fn as_slice<'cx, 'a, C>(&self, cx: &'a C) -> &'a [Self::Item]
    where
        C: Context<'cx>
; fn as_mut_slice<'cx, 'a, C>(&mut self, cx: &'a mut C) -> &'a mut [Self::Item]
    where
        C: Context<'cx>
; fn try_borrow<'cx, 'a, C>(
        &self,
        lock: &'a Lock<'_, C>
    ) -> Result<Ref<'a, Self::Item>, BorrowError>
    where
        C: Context<'cx>
; fn try_borrow_mut<'cx, 'a, C>(
        &mut self,
        lock: &'a Lock<'_, C>
    ) -> Result<RefMut<'a, Self::Item>, BorrowError>
    where
        C: Context<'cx>
; fn size<'cx, C>(&self, cx: &mut C) -> usize
    where
        C: Context<'cx>
; fn from_slice<'cx, C>(
        cx: &mut C,
        slice: &[Self::Item]
    ) -> JsResult<'cx, Self>
    where
        C: Context<'cx>
; }
Expand description

A trait allowing Rust to borrow binary data from the memory buffer of JavaScript typed arrays.

This trait provides both statically and dynamically checked borrowing. As usual in Rust, mutable borrows are guaranteed not to overlap with other borrows.

Example

use neon::types::buffer::TypedArray;

fn double(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    let mut array: Handle<JsUint32Array> = cx.argument(0)?;

    for elem in array.as_mut_slice(&mut cx).iter_mut() {
        *elem *= 2;
    }

    Ok(cx.undefined())
}

Required Associated Types

Required Methods

Statically checked immutable borrow of binary data.

This may not be used if a mutable borrow is in scope. For the dynamically checked variant see TypedArray::try_borrow.

Statically checked mutable borrow of binary data.

This may not be used if any other borrow is in scope. For the dynamically checked variant see TypedArray::try_borrow_mut.

Dynamically checked immutable borrow of binary data, returning an error if the the borrow would overlap with a mutable borrow.

The borrow lasts until Ref exits scope.

This is the dynamically checked version of TypedArray::as_slice.

Dynamically checked mutable borrow of binary data, returning an error if the the borrow would overlap with an active borrow.

The borrow lasts until RefMut exits scope.

This is the dynamically checked version of TypedArray::as_mut_slice.

Returns the size, in bytes, of the allocated binary data.

Constructs an instance from a slice by copying its contents.

Implementors