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.
```rust // Or you can use this instead to avoid needing to import // the macro throughout your crate // #[macrouse] // extern crate wasmbindgenstruct; use wasmbindgenstruct::wasmbindgen_struct;
// module
here means this type comes from the JS "my-module" module
// with the class name aAnotherType
struct MyJsType {
// field names are automatically converted to camelCase
fielda: String,
// ...but can be manually specified if needed
#[opts(jsname = "fieldb")]
fieldb: String,
}
impl MyJsType {
// Automatically gets the static_method_of = MyJsType
applied,
// as well as catch
due to the Result
fn apple() -> Result
// async
functions in wasm-bindgen
can only currently return
// ()
, JsValue
, or Result<_, JsValue>, where
is one of
// the two previously mentioned types. We use
MapValue
// to tell the macro that the binding method should return
T,
// but we'll map the value to
U`
async fn oranges(&self) -> MapValue
The above expands to:
```rust
// For getters/setters
extern "C" { #[wasmbindgen(jsname = "AnotherType")] type MyJsType;
#[wasmbindgen( method, getter, jsclass = "AnotherType", jsname = "fieldA" )] fn fielda(this: &MyJsType) -> String;
#[wasmbindgen( method, setter, jsclass = "AnotherType", jsname = "fieldA" )] fn setfield_a(this: &MyJsType, value: String);
#[wasmbindgen( method, getter, jsclass = "AnotherType", jsname = "fieldb" )] fn fieldb(this: &MyJsType) -> String; #[wasmbindgen( method, setter, jsclass = "AnotherType", jsname = "fieldb" )] fn setfield_b(this: &MyJsType, value: String); }
// And for methods
impl MyJsType {
fn apple() -> Result
#[opts(...)]
on structsdbg: bool
Show the formatted output of the macro as a warning
on: Type
Allows using an existing type with the struct syntax.
```rust
extern "C" { type JsType; }
// Struct can be named anything, it doesn't matter struct JsType { field_1: bool, } ```
bool
bool
bool
Lit
[Lit1, Lit2, Lit3, ...]
Lit
Lit
<unknown>
Unknown attributes, such as doc comments, are forwarded to
the type MyType
declaration, or ignored in the case of using
on
.
#[opts(...)]
on struct fields#[opts(...)]
on impl
dbg: bool
Show the formatted output of the macro as a warning
Lit
[Lit1, Lit2, Lit3, ...]
Lit
Lit
#[opts(...)]
on impl
methodsbool
bool
bool
bool
bool
bool
bool
bool
Lit
bool
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.
```rust
struct JsType;
impl JsType {
async fn getasyncstring(&self) -> MapValue
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.