From d1b22993405408ea19ace44f7f828eb5c5216585 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 13 Aug 2018 17:08:18 -0700 Subject: [PATCH] guide: Add `Box<[JsValue]>` example to supported types section --- .../boxed_js_value_slice.js | 22 +++++++++++++++++++ .../src/boxed_js_value_slice.rs | 17 ++++++++++++++ .../guide-supported-types-examples/src/lib.rs | 1 + guide/src/reference/types.md | 12 ++++++++++ 4 files changed, 52 insertions(+) create mode 100644 examples/guide-supported-types-examples/boxed_js_value_slice.js create mode 100644 examples/guide-supported-types-examples/src/boxed_js_value_slice.rs diff --git a/examples/guide-supported-types-examples/boxed_js_value_slice.js b/examples/guide-supported-types-examples/boxed_js_value_slice.js new file mode 100644 index 000000000..ade879689 --- /dev/null +++ b/examples/guide-supported-types-examples/boxed_js_value_slice.js @@ -0,0 +1,22 @@ +import { + take_boxed_js_value_slice_by_value, + return_boxed_js_value_slice, + take_option_boxed_js_value_slice, + return_option_boxed_js_value_slice, +} from './guide_supported_types_examples'; + +take_boxed_js_value_slice_by_value([null, true, 2, {}, []]); + +let values = return_boxed_js_value_slice(); +console.log(values instanceof Array); // true + +take_option_boxed_js_value_slice(null); +take_option_boxed_js_value_slice(undefined); +take_option_boxed_js_value_slice([1, 2, 3]); + +let maybeValues = return_option_boxed_js_value_slice(); +if (maybeValues == null) { + // ... +} else { + console.log(maybeValues instanceof Array); // true +} diff --git a/examples/guide-supported-types-examples/src/boxed_js_value_slice.rs b/examples/guide-supported-types-examples/src/boxed_js_value_slice.rs new file mode 100644 index 000000000..152482fca --- /dev/null +++ b/examples/guide-supported-types-examples/src/boxed_js_value_slice.rs @@ -0,0 +1,17 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn take_boxed_js_value_slice_by_value(x: Box<[JsValue]>) {} + +#[wasm_bindgen] +pub fn return_boxed_js_value_slice() -> Box<[JsValue]> { + vec![JsValue::NULL, JsValue::UNDEFINED].into_boxed_slice() +} + +#[wasm_bindgen] +pub fn take_option_boxed_js_value_slice(x: Option>) {} + +#[wasm_bindgen] +pub fn return_option_boxed_js_value_slice() -> Option> { + None +} diff --git a/examples/guide-supported-types-examples/src/lib.rs b/examples/guide-supported-types-examples/src/lib.rs index cfaea27e0..1fef349a1 100755 --- a/examples/guide-supported-types-examples/src/lib.rs +++ b/examples/guide-supported-types-examples/src/lib.rs @@ -10,3 +10,4 @@ pub mod string; pub mod char; pub mod bool; pub mod js_value; +pub mod boxed_js_value_slice; diff --git a/guide/src/reference/types.md b/guide/src/reference/types.md index e28105eea..c8f82339f 100644 --- a/guide/src/reference/types.md +++ b/guide/src/reference/types.md @@ -145,6 +145,18 @@ garbage-collected heap and the Wasm linear memory with `TextDecoder` and |:---:|:---:|:---:|:---:|:---:|:---:|:---:| | Yes | No | No | Yes | Yes | Yes | A JavaScript `Array` object | +### Example Rust Usage + +```rust +{{#include ../../../examples/guide-supported-types-examples/src/boxed_js_value_slice.rs}} +``` + +### Example JavaScript Usage + +```js +{{#include ../../../examples/guide-supported-types-examples/boxed_js_value_slice.js}} +``` + ## `*const T` `*mut T` | `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option` parameter | `Option` return value | JavaScript representation |