JSTypedArray

Struct JSTypedArray 

Source
pub struct JSTypedArray { /* private fields */ }
Expand description

A JavaScript Typed Array.

A Typed Array is a special JavaScript object that represent a family of buffer views. Learn more by reading the documentation.

Implementations§

Source§

impl JSTypedArray

Source

pub fn ty(&self) -> Result<JSTypedArrayType, JSException>

Returns a value of type JSTypedArrayType that identifies value’s Typed Array type, or JSTypedArrayType::None if the value is not a Typed Array object.

let ctx = JSContext::default();
let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
    .unwrap()
    .as_typed_array()
    .unwrap();
assert_eq!(array.ty().unwrap(), JSTypedArrayType::Uint8Array);
Source

pub fn len(&self) -> Result<usize, JSException>

Returns the length of the Typed Array.

let ctx = JSContext::default();
let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
    .unwrap()
    .as_typed_array()
    .unwrap();
assert_eq!(array.len().unwrap(), 5);
Source

pub fn byte_offset(&self) -> Result<usize, JSException>

Returns the byte offset of the Typed Array.

The byte offset is the offset used when a Typed Array is created from another Typed Array, it’s a “subview” that can start from an offset, up to a certain length, which is the byte length.

let ctx = JSContext::default();
let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 3)", None, "foo.js", 1)
    .unwrap()
    .as_typed_array()
    .unwrap();
assert_eq!(array.byte_offset().unwrap(), 3);
Source

pub fn byte_length(&self) -> Result<usize, JSException>

Returns the byte length of the Typed Array.

The byte length is the length used when a Typed Array is created from another Typed Array, it’s a “subview” that can start from an offset, up to a certain length, which is the byte length.

let ctx = JSContext::default();
let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 2)", None, "foo.js", 1)
    .unwrap()
    .as_typed_array()
    .unwrap();
assert_eq!(array.byte_length().unwrap(), 2);
Source

pub unsafe fn as_mut_slice(&mut self) -> Result<&mut [u8], JSException>

Returns a mutable slice of the underlying buffer represented by the Typed Array.

§Safety

The pointer of the slice returned by this function is temporary and is not guaranteed to remain valid across JavaScriptCore API calls.

§Example
let ctx = JSContext::default();

/// Create a Typed Array from the Rust API.
let mut bytes = vec![1u8, 2, 3, 4, 5];
let array_as_value =
    unsafe { JSValue::new_typed_array_with_bytes(&ctx, bytes.as_mut_slice()) }.unwrap();
let mut array = array_as_value.as_typed_array().unwrap();

ctx.global_object().unwrap().set_property("array", array_as_value).unwrap();

/// Create a sub-Typed Array from `array` in JavaScript.
let mut sub_array = evaluate_script(
    &ctx,
    "new Uint8Array(array.buffer, 1, 3)",
    None,
    "foo.js",
    1,
)
    .unwrap()
    .as_typed_array()
    .unwrap();

let sub_slice = unsafe { sub_array.as_mut_slice() }.unwrap();

// Items are untouched.
assert_eq!(sub_slice, &[2, 3, 4]);
assert_eq!(bytes, &[1, 2, 3, 4, 5]);

// Now let's mutate them.
sub_slice[0] = 12;
sub_slice[2] = 14;

// See, they are mutated.
assert_eq!(sub_slice, &[12, 3, 14]);
assert_eq!(bytes, &[1, 12, 3, 14, 5]);
Source

pub fn to_vec(&self) -> Result<Vec<u8>, JSException>

Returns a Vec (so a copy) of the underlying buffer represented by the Typed Array.

let ctx = JSContext::default();

let mut array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 3)", None, "foo.js", 1)
    .unwrap()
    .as_typed_array()
    .unwrap();

assert_eq!(array.to_vec().unwrap(), &[2, 3, 4]);

Trait Implementations§

Source§

impl From<&JSTypedArray> for JSObject

Source§

fn from(array: &JSTypedArray) -> Self

Converts to this type from the input type.
Source§

impl From<JSTypedArray> for JSObject

Source§

fn from(array: JSTypedArray) -> Self

Converts to this type from the input type.

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 = 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.