From b1e3101fd4be5e69af24e05caddbdfae9474b329 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 14 Aug 2018 17:42:47 -0700 Subject: [PATCH] guide: Add examples for number slices --- .../guide-supported-types-examples/bootstrap.js | 1 + .../number_slices.js | 9 +++++++++ .../guide-supported-types-examples/src/lib.rs | 1 + .../src/number_slices.rs | 7 +++++++ guide/src/reference/types.md | 17 +++++++++++++---- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 examples/guide-supported-types-examples/number_slices.js create mode 100644 examples/guide-supported-types-examples/src/number_slices.rs diff --git a/examples/guide-supported-types-examples/bootstrap.js b/examples/guide-supported-types-examples/bootstrap.js index 59a2f964..1924d27f 100644 --- a/examples/guide-supported-types-examples/bootstrap.js +++ b/examples/guide-supported-types-examples/bootstrap.js @@ -9,3 +9,4 @@ import * as boxed_js_value_slice from './boxed_js_value_slice.js'; import * as pointers from './pointers.js'; import * as numbers from './numbers.js'; import * as boxed_number_slices from './boxed_number_slices.js'; +import * as number_slices from './number_slices.js'; diff --git a/examples/guide-supported-types-examples/number_slices.js b/examples/guide-supported-types-examples/number_slices.js new file mode 100644 index 00000000..e66b7971 --- /dev/null +++ b/examples/guide-supported-types-examples/number_slices.js @@ -0,0 +1,9 @@ +import { + take_number_slice_by_value, + return_number_slice, + take_option_number_slice, + return_option_number_slice, +} from './guide_supported_types_examples'; + +take_number_slice_by_shared_ref(new Float64Array(100)); +take_number_slice_by_exclusive_ref(new Uint8Array(100)); diff --git a/examples/guide-supported-types-examples/src/lib.rs b/examples/guide-supported-types-examples/src/lib.rs index d1aa1d13..9dbfdf48 100755 --- a/examples/guide-supported-types-examples/src/lib.rs +++ b/examples/guide-supported-types-examples/src/lib.rs @@ -14,3 +14,4 @@ pub mod boxed_js_value_slice; pub mod pointers; pub mod numbers; pub mod boxed_number_slices; +pub mod number_slices; diff --git a/examples/guide-supported-types-examples/src/number_slices.rs b/examples/guide-supported-types-examples/src/number_slices.rs new file mode 100644 index 00000000..c379b39e --- /dev/null +++ b/examples/guide-supported-types-examples/src/number_slices.rs @@ -0,0 +1,7 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn take_number_slice_by_shared_ref(x: &[f64]) {} + +#[wasm_bindgen] +pub fn take_number_slice_by_exclusive_ref(x: &mut [u8]) {} diff --git a/guide/src/reference/types.md b/guide/src/reference/types.md index 846ddf4b..f063e7ce 100644 --- a/guide/src/reference/types.md +++ b/guide/src/reference/types.md @@ -217,9 +217,18 @@ versa when receiving a JavaScript `TypedArray` as a boxed slice in Rust. ## `[u8]` `[i8]` `[u16]` `[i16]` `[u32]` `[i32]` `[u64]` `[i64]` `[f32]` `[f64]` -| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option` parameter | `Option` return value | JavaScript representation | +| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<&T>` parameter | `Option` return value | JavaScript representation | |:---:|:---:|:---:|:---:|:---:|:---:|:---:| -| No | Yes | Yes | No | Yes | No | A JavaScript `TypedArray` view of the Wasm memory for the boxed slice of the appropriate type (`Int32Array`, `Uint8Array`, etc) | +| No | Yes | Yes | No | No | No | A JavaScript `TypedArray` view of the Wasm memory for the boxed slice of the appropriate type (`Int32Array`, `Uint8Array`, etc) | -Note that this does ***not*** copy the whole slice of memory back and forth into -the JavaScript heap from the Wasm linear memory. +### Example Rust Usage + +```rust +{{#include ../../../examples/guide-supported-types-examples/src/number_slices.rs}} +``` + +### Example JavaScript Usage + +```js +{{#include ../../../examples/guide-supported-types-examples/number_slices.js}} +```