guide: Add examples for working with numbers to types section

This commit is contained in:
Nick Fitzgerald 2018-08-14 15:45:25 -07:00
parent 602b63c1b0
commit 8043baac69
5 changed files with 60 additions and 6 deletions

View File

@ -1,2 +1,10 @@
import * as imported_types from './imported_types.js';
import * as exported_types from './exported_types.js';
import * as str from './str.js';
import * as string from './string.js';
import * as char from './char.js';
import * as bool from './bool.js';
import * as js_value from './js_value.js';
import * as boxed_js_value_slice from './boxed_js_value_slice.js';
import * as pointers from './pointers.js';
import * as numbers from './numbers.js';

View File

@ -0,0 +1,22 @@
import {
take_number_by_value,
return_number,
take_option_number,
return_option_number,
} from './guide_supported_types_examples';
take_number_by_value(42);
let x = return_number();
console.log(typeof x); // "number"
take_option_number(null);
take_option_number(undefined);
take_option_number(13);
let y = return_option_number();
if (y == null) {
// ...
} else {
console.log(typeof y); // "number"
}

View File

@ -12,3 +12,4 @@ pub mod bool;
pub mod js_value;
pub mod boxed_js_value_slice;
pub mod pointers;
pub mod numbers;

View File

@ -0,0 +1,17 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn take_number_by_value(x: u32) {}
#[wasm_bindgen]
pub fn return_number() -> f64 {
42.0
}
#[wasm_bindgen]
pub fn take_option_number(x: Option<u8>) {}
#[wasm_bindgen]
pub fn return_option_number() -> Option<i16> {
Some(-300)
}

View File

@ -175,17 +175,23 @@ garbage-collected heap and the Wasm linear memory with `TextDecoder` and
{{#include ../../../examples/guide-supported-types-examples/pointers.js}}
```
## `u8` `i8` `u16` `i16` `u64` `i64` `isize` `size`
## `u8` `i8` `u16` `i16` `u32` `i32` `u64` `i64` `isize` `usize` `f32` `f64`
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| Yes | No | No | Yes | No | No | A JavaScript number value |
| Yes | No | No | Yes | Yes | Yes | A JavaScript number value |
## `u32` `i32` `f32` `f64`
### Example Rust Usage
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| Yes | Yes | Yes | Yes | No | No | A JavaScript number value |
```rust
{{#include ../../../examples/guide-supported-types-examples/src/numbers.rs}}
```
### Example JavaScript Usage
```js
{{#include ../../../examples/guide-supported-types-examples/numbers.js}}
```
## `Box<[u8]>` `Box<[i8]>` `Box<[u16]>` `Box<[i16]>` `Box<[u32]>` `Box<[i32]>` `Box<[u64]>` `Box<[i64]>` `Box<[f32]>` `Box<[f64]>`