带框数字切片: Box<[u8]>Box<[i8]>Box<[u16]>Box<[i16]>Box<[u32]>Box<[i32]>Box<[u64]>Box<[i64]>Box<[f32]>Box<[f64]>

T 参数&T 参数&mut T 参数T 返回值Option<T> 参数Option<T> 返回值JavaScript 表示
一个 JavaScript TypedArray,类型与之对应(Int32ArrayUint8Array 等)

请注意,当将带框切片返回给 JavaScript 时,切片的内容将从 Wasm 线性内存复制到 JavaScript TypedArray 中,反之亦然,当在 Rust 中接收 JavaScript TypedArray 作为带框切片时。

Rust 使用示例

#![allow(unused)]
fn main() {
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn take_boxed_number_slice_by_value(x: Box<[f64]>) {}

#[wasm_bindgen]
pub fn return_boxed_number_slice() -> Box<[u32]> {
    (0..42).collect::<Vec<u32>>().into_boxed_slice()
}

#[wasm_bindgen]
pub fn take_option_boxed_number_slice(x: Option<Box<[u8]>>) {}

#[wasm_bindgen]
pub fn return_option_boxed_number_slice() -> Option<Box<[i32]>> {
    None
}
}

JavaScript 使用示例

import {
  take_boxed_number_slice_by_value,
  return_boxed_number_slice,
  take_option_boxed_number_slice,
  return_option_boxed_number_slice,
} from './guide_supported_types_examples';

take_boxed_number_slice_by_value(new Uint8Array(100));

let x = return_boxed_number_slice();
console.log(x instanceof Uint32Array); // true

take_option_boxed_number_slice(null);
take_option_boxed_number_slice(undefined);
take_option_boxed_number_slice(new Int16Array(256));

let y = return_option_boxed_number_slice();
if (y == null) {
  // ...
} else {
  console.log(x instanceof Int32Array); // true
}