mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-27 03:55:20 +03:00
guide: Add working with pointers example to types section
This commit is contained in:
parent
79f9d966e5
commit
602b63c1b0
12
examples/guide-supported-types-examples/pointers.js
Normal file
12
examples/guide-supported-types-examples/pointers.js
Normal file
@ -0,0 +1,12 @@
|
||||
import {
|
||||
take_pointer_by_value,
|
||||
return_pointer,
|
||||
} from './guide_supported_types_examples';
|
||||
import { memory } from './guide_supported_types_examples_bg';
|
||||
|
||||
let ptr = return_pointer();
|
||||
let buf = new Uint8Array(memory.buffer);
|
||||
let value = buf[ptr];
|
||||
console.log(`The byte at the ${ptr} address is ${value}`);
|
||||
|
||||
take_pointer_by_value(ptr);
|
@ -11,3 +11,4 @@ pub mod char;
|
||||
pub mod bool;
|
||||
pub mod js_value;
|
||||
pub mod boxed_js_value_slice;
|
||||
pub mod pointers;
|
||||
|
10
examples/guide-supported-types-examples/src/pointers.rs
Normal file
10
examples/guide-supported-types-examples/src/pointers.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use std::ptr;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn take_pointer_by_value(x: *mut u8) {}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn return_pointer() -> *mut u8 {
|
||||
ptr::null_mut()
|
||||
}
|
@ -163,6 +163,18 @@ garbage-collected heap and the Wasm linear memory with `TextDecoder` and
|
||||
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|
||||
| Yes | No | No | Yes | No | No | A JavaScript number value |
|
||||
|
||||
### Example Rust Usage
|
||||
|
||||
```rust
|
||||
{{#include ../../../examples/guide-supported-types-examples/src/pointers.rs}}
|
||||
```
|
||||
|
||||
### Example JavaScript Usage
|
||||
|
||||
```js
|
||||
{{#include ../../../examples/guide-supported-types-examples/pointers.js}}
|
||||
```
|
||||
|
||||
## `u8` `i8` `u16` `i16` `u64` `i64` `isize` `size`
|
||||
|
||||
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
|
||||
|
Loading…
Reference in New Issue
Block a user