Trait neon::types::buffer::TypedArray

source ·
pub trait TypedArray: Value {
    type Item: Binary;

    // Required methods
    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§

source

fn as_slice<'cx, 'a, C>(&self, cx: &'a C) -> &'a [Self::Item]
where C: Context<'cx>,

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.

source

fn as_mut_slice<'cx, 'a, C>(&mut self, cx: &'a mut C) -> &'a mut [Self::Item]
where C: Context<'cx>,

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.

source

fn try_borrow<'cx, 'a, C>( &self, lock: &'a Lock<'_, C> ) -> Result<Ref<'a, Self::Item>, BorrowError>
where C: Context<'cx>,

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.

source

fn try_borrow_mut<'cx, 'a, C>( &mut self, lock: &'a Lock<'_, C> ) -> Result<RefMut<'a, Self::Item>, BorrowError>
where C: Context<'cx>,

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.

source

fn size<'cx, C>(&self, cx: &mut C) -> usize
where C: Context<'cx>,

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

source

fn from_slice<'cx, C>(cx: &mut C, slice: &[Self::Item]) -> JsResult<'cx, Self>
where C: Context<'cx>,

Constructs an instance from a slice by copying its contents.

Object Safety§

This trait is not object safe.

Implementors§