-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Option<*const|mut T>
and NonNull<T>
- Loading branch information
Showing
9 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::ptr; | ||
use std::ptr::NonNull; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub unsafe fn take_pointer_by_value(x: Option<NonNull<u8>>) { | ||
Box::from_raw(x.unwrap().as_ptr()); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn return_pointer() -> Option<NonNull<u8>> { | ||
NonNull::from(Box::new(42).leak()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# `*const T` and `*mut T` | ||
|
||
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation | | ||
|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | ||
| No | No | No | Yes | Yes | Yes | A JavaScript number value | | ||
|
||
## Example Rust Usage | ||
|
||
```rust | ||
{{#include ../../../../examples/guide-supported-types-examples/src/non_null.rs}} | ||
``` | ||
|
||
## Example JavaScript Usage | ||
|
||
```js | ||
{{#include ../../../../examples/guide-supported-types-examples/non_null.js}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters