装箱的数字切片:Box<[u8]>
、Box<[i8]>
、Box<[u16]>
、Box<[i16]>
、Box<[u32]>
、Box<[i32]>
、Box<[u64]>
、Box<[i64]>
、Box<[f32]>
、Box<[f64]>
、Box<[MaybeUninit<u8>]>
、Box<[MaybeUninit<i8>]>
、Box<[MaybeUninit<u16>]>
、Box<[MaybeUninit<i16>]>
、Box<[MaybeUninit<u32>]>
、Box<[MaybeUninit<i32>]>
、Box<[MaybeUninit<u64>]>
、Box<[MaybeUninit<i64>]>
、Box<[MaybeUninit<f32>]>
和 Box<[MaybeUninit<f64>]>
T 参数 | &T 参数 | &mut T 参数 | T 返回值 | Option<T> 参数 | Option<T> 返回值 | JavaScript 表示 |
---|---|---|---|---|---|---|
是 | 否 | 否 | 是 | 是 | 是 | 相应类型的 JavaScript TypedArray (Int32Array 、Uint8Array 等...) |
注意: 当将装箱的切片返回给 JavaScript 时,切片的内容会从 Wasm 线性内存复制到 JavaScript
TypedArray
中;反之,当在 Rust 中接收 JavaScriptTypedArray
作为装箱的切片时,也会进行复制。
注意: 数值型的
MaybeUninit<T>
在从 Rust 传输到 JS 以及从 JS 传输到 Rust 时,始终可以假定为已初始化。 但是,来自 Rust 的未初始化的值可能包含未指定的值。
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
}