-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add page to guide about enums #3693
base: main
Are you sure you want to change the base?
Changes from 8 commits
cc472e0
b62ba89
36bb8c4
f2d75ae
184057b
813a55d
c049ca2
83619ac
8368502
0b1f61b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
take_number, | ||
take_string, | ||
take_number_option, | ||
take_string_option, | ||
return_number, | ||
return_string, | ||
return_number_option, | ||
return_string_option, | ||
NumberEnum, | ||
// nothing generated for StringEnum :( | ||
} from "./guide_supported_types_examples"; | ||
|
||
take_number(NumberEnum.Foo); | ||
take_string("spam"); | ||
|
||
take_number_option(NumberEnum.Bar); | ||
take_number_option(undefined); | ||
|
||
take_string_option("eggs"); | ||
take_string_option(undefined); | ||
|
||
return_number(); // typed as `NumberEnum` | ||
return_string(); // typed as `any` | ||
|
||
return_number_option(); // typed as `NumberEnum | undefined` | ||
return_string_option(); // typed as `any | undefined` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub enum NumberEnum { | ||
// numbers are optional; default ones will be generated if not provided | ||
Foo = 0, | ||
Bar = 1, | ||
Baz = 2, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub enum StringEnum { | ||
Spam = "spam", | ||
Eggs = "eggs", | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn take_number(x: NumberEnum) {} | ||
|
||
#[wasm_bindgen] | ||
pub fn take_string(x: StringEnum) {} | ||
|
||
#[wasm_bindgen] | ||
pub fn take_number_option(x: Option<NumberEnum>) {} | ||
|
||
#[wasm_bindgen] | ||
pub fn take_string_option(x: Option<StringEnum>) {} | ||
|
||
#[wasm_bindgen] | ||
pub fn return_number() -> NumberEnum { | ||
todo!() | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn return_string() -> StringEnum { | ||
todo!() | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn return_number_option() -> Option<NumberEnum> { | ||
todo!() | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn return_string_option() -> Option<StringEnum> { | ||
todo!() | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||||||
# enum | ||||||||||
|
||||||||||
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation | | ||||||||||
| :-----------: | :------------: | :----------------: | :--------------: | :-------------------: | :----------------------: | :-----------------------: | | ||||||||||
| Yes | No | No | No | Yes | Yes | `string` or `number` | | ||||||||||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
## Example Rust Usage | ||||||||||
|
||||||||||
```rust | ||||||||||
{{#include ../../../../examples/guide-supported-types-examples/src/enum.rs}} | ||||||||||
``` | ||||||||||
|
||||||||||
## Example JavaScript Usage | ||||||||||
|
||||||||||
```js | ||||||||||
{{#include ../../../../examples/guide-supported-types-examples/enum.js}} | ||||||||||
``` | ||||||||||
|
||||||||||
## TypeScript | ||||||||||
|
||||||||||
Unfortunately, string enums don't fully work yet; no TypeScript is generated for the enum itself and functions using them accept or return `any`. | ||||||||||
They work correctly, it's just there's no type hints. | ||||||||||
See [Issue #3057](https://github.com/rustwasm/wasm-bindgen/issues/3057) | ||||||||||
Comment on lines
+21
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's keep the language neutral like in the rest of the documentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to link it so it doesn't go out of date. i agree on the wording change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like if string enums get fixed and the documentation doesn't get updated, hopefully someone will open the issue, see that it's completed, and realize "oh, the documentation must be outdated" |
||||||||||
|
||||||||||
The generated TypeScript declarations for the above: | ||||||||||
|
||||||||||
<!-- remember to keep this up to date! copy enum.rs (above) into the lib.rs file of a new wasm-bindgen crate; use `wasm-pack build`; then copy pkg/testcrate.d.ts. also ran it through a formatter. --> | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep this consistent in the future: is the formatter really necessary? |
||||||||||
|
||||||||||
```ts | ||||||||||
/** | ||||||||||
* @param {NumberEnum} x | ||||||||||
*/ | ||||||||||
export function take_number(x: NumberEnum): void; | ||||||||||
/** | ||||||||||
* @param {any} x | ||||||||||
*/ | ||||||||||
export function take_string(x: any): void; | ||||||||||
/** | ||||||||||
* @param {NumberEnum | undefined} [x] | ||||||||||
*/ | ||||||||||
export function take_number_option(x?: NumberEnum): void; | ||||||||||
/** | ||||||||||
* @param {any | undefined} [x] | ||||||||||
*/ | ||||||||||
export function take_string_option(x?: any): void; | ||||||||||
/** | ||||||||||
* @returns {NumberEnum} | ||||||||||
*/ | ||||||||||
export function return_number(): NumberEnum; | ||||||||||
/** | ||||||||||
* @returns {any} | ||||||||||
*/ | ||||||||||
export function return_string(): any; | ||||||||||
/** | ||||||||||
* @returns {NumberEnum | undefined} | ||||||||||
*/ | ||||||||||
export function return_number_option(): NumberEnum | undefined; | ||||||||||
/** | ||||||||||
* @returns {any | undefined} | ||||||||||
*/ | ||||||||||
export function return_string_option(): any | undefined; | ||||||||||
/** | ||||||||||
*/ | ||||||||||
export enum NumberEnum { | ||||||||||
Foo = 0, | ||||||||||
Bar = 1, | ||||||||||
Baz = 2, | ||||||||||
} | ||||||||||
|
||||||||||
// no types generated for StringEnum (yet) :( | ||||||||||
// see the note above | ||||||||||
Comment on lines
+69
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think this is necessary given the above note. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worry probably too much about someone scrolling past the prose and just reading the examples |
||||||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add the spaces here?
Seems inconsistent with the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk vscode auto-formatter just doing whatever it wants