A crate to make working with wasm-bindgen
easier.
This crate contains the wasm_bindgen_struct
macro which
can be used to declare wasm_bindgen
types, and implement
getters/setter as if they were normal rust structs. It can
also be used to implement methods for the type, including inline
mapping to overcome wasm-bindgen-futures
limitations.
Example
// Or you can use this instead to avoid needing to import
// the macro throughout your crate
// #[macro_use]
// extern crate wasm_bindgen_struct;
use wasm_bindgen_struct;
// `module` here means this type comes from the JS "my-module" module
// with the class name `aAnotherType`
The above expands to:
// For getters/setters
extern "C"
// And for methods
#[opts(...)]
on structs
-
dbg:
bool
Show the formatted output of the macro as a warning
-
on:
Type
Allows using an existing type with the struct syntax.
Example
extern "C" // Struct can be named anything, it doesn't matter
-
<unknown>
Unknown attributes, such as doc comments, are forwarded to the
type MyType
declaration, or ignored in the case of usingon
.
#[opts(...)]
on struct fields
#[opts(...)]
on impl
-
dbg: bool
Show the formatted output of the macro as a warning
#[opts(...)]
on impl
methods
- constructor:
bool
- final_:
bool
- structural:
bool
- getter:
bool
- setter:
bool
- indexing_getter:
bool
- indexing_setter:
bool
- indexing_deleter:
bool
- js_name:
Lit
- variadic:
bool
Treatment of Special Types
-
Result<T, E>
Implies
catch
. -
MapValue<T, U>
Allows specifing the types of the generated
wasm-bindgen
binding, as well as the actual method type. This allows arbitrary transformations between the bound method and the output method.Example
;
Important note
MapValue
is a pseudo-type which only exists within the macro's scope. i.e., this type does not exist outside of#[wasm_bindgen_struct] impl { /* ... */ }
and therefore does not need to be imported.