Skip to content

add page to guide about enums #3693

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/guide-supported-types-examples/enums.js
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,
StringEnum,
} from "./guide_supported_types_examples";

take_enum_number(NumberEnum.Foo);
take_enum_string("spam");

take_enum_number_option(NumberEnum.Bar);
take_enum_number_option(undefined);

take_enum_string_option("eggs");
take_enum_string_option(undefined);

return_enum_number(); // -> `NumberEnum`
return_enum_string(); // -> `StringEnum`

return_enum_number_option(); // -> `NumberEnum | undefined`
return_enum_string_option(); // -> `StringEnum | undefined`
47 changes: 47 additions & 0 deletions examples/guide-supported-types-examples/src/enums.rs
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_enum_number(x: NumberEnum) {}

#[wasm_bindgen]
pub fn take_enum_string(x: StringEnum) {}

#[wasm_bindgen]
pub fn take_enum_number_option(x: Option<NumberEnum>) {}

#[wasm_bindgen]
pub fn take_enum_string_option(x: Option<StringEnum>) {}

#[wasm_bindgen]
pub fn return_enum_number() -> NumberEnum {
todo!()
}

#[wasm_bindgen]
pub fn return_enum_string() -> StringEnum {
todo!()
}

#[wasm_bindgen]
pub fn return_enum_number_option() -> Option<NumberEnum> {
todo!()
}

#[wasm_bindgen]
pub fn return_enum_string_option() -> Option<StringEnum> {
todo!()
}
22 changes: 0 additions & 22 deletions examples/guide-supported-types-examples/src/imported_types.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub enum NumberEnum {
Foo = 0,
Bar = 1,
Qux = 2,
}

#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub enum StringEnum {
Foo = "foo",
Bar = "bar",
Qux = "qux",
}

#[wasm_bindgen]
pub struct Struct {
pub number: NumberEnum,
pub string: StringEnum,
}

#[wasm_bindgen]
extern "C" {
pub type SomeJsType;
Expand Down
1 change: 1 addition & 0 deletions examples/guide-supported-types-examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod bool;
pub mod boxed_js_value_slice;
pub mod boxed_number_slices;
pub mod char;
pub mod enums;
pub mod exported_types;
pub mod imported_types;
pub mod js_value;
Expand Down
1 change: 1 addition & 0 deletions guide/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- [`char`](./reference/types/char.md)
- [`str`](./reference/types/str.md)
- [`String`](./reference/types/string.md)
- [`enum`](./reference/types/enum.md)
- [Number Slices](./reference/types/number-slices.md)
- [Boxed Number Slices](./reference/types/boxed-number-slices.md)
- [`Result<T, E>`](./reference/types/result.md)
Expand Down
19 changes: 19 additions & 0 deletions guide/src/reference/types/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# enum

| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
| :-----------: | :------------: | :----------------: | :--------------: | :-------------------: | :----------------------: | :-----------------------: |
| Yes | No | No | Yes | Yes | Yes | `string` or `number` |

Only C-style enums are currently supported; see

## Example Rust Usage

```rust
{{#include ../../../../examples/guide-supported-types-examples/src/enums.rs}}
```

## Example JavaScript Usage

```js
{{#include ../../../../examples/guide-supported-types-examples/enums.js}}
```
Loading