Exported struct Whatever Rust Types
T parameter | &T parameter | &mut T parameter | T return value | Option<T> parameter | Option<T> return value | JavaScript representation |
|---|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes | Instances of a wasm-bindgen-generated JavaScript class Whatever { ... } |
Note: Public fields implementing
Copyhave automatically generated getters/setters. To generate getters/setters for non-Copypublic fields, use#[wasm_bindgen(getter_with_clone)]for the struct or implement getters/setters manually.
Passing an instance by value (T or Option<T> parameter) moves it into
Rust: the value is not copied, and the JavaScript object is left empty, as if
.free() had been called on it. Any later use of that object, whether passing
it to another function or accessing its fields and methods, throws an error
(Attempt to use a moved value when built with --debug, null pointer passed to rust otherwise). Passing by reference (&T or &mut T) leaves the
JavaScript object usable.
Exported functions can use generic JavaScript types with concrete type parameters (like Promise<Number>), but cannot have their own generic type parameters.
Example Rust Usage
#![allow(unused)]
#![allow(clippy::clone_on_copy)]
fn main() {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct ExportedNamedStruct {
// pub value: String, // This won't work. See working example below.
pub inner: u32,
}
#[wasm_bindgen(getter_with_clone)]
pub struct ExportedNamedStructNonCopy {
pub non_copy_value: String,
pub copy_value: u32,
}
#[wasm_bindgen]
pub fn named_struct_by_value(x: ExportedNamedStruct) {}
#[wasm_bindgen]
pub fn named_struct_by_shared_ref(x: &ExportedNamedStruct) {}
#[wasm_bindgen]
pub fn named_struct_by_exclusive_ref(x: &mut ExportedNamedStruct) {}
#[wasm_bindgen]
pub fn return_named_struct(inner: u32) -> ExportedNamedStruct {
ExportedNamedStruct { inner }
}
#[wasm_bindgen]
pub fn named_struct_by_optional_value(x: Option<ExportedNamedStruct>) {}
#[wasm_bindgen]
pub fn return_optional_named_struct(inner: u32) -> Option<ExportedNamedStruct> {
Some(ExportedNamedStruct { inner })
}
#[wasm_bindgen]
pub struct ExportedTupleStruct(pub u32, pub u32);
#[wasm_bindgen]
pub fn return_tuple_struct(x: u32, y: u32) -> ExportedTupleStruct {
ExportedTupleStruct(x, y)
}
}
Example JavaScript Usage
import {
ExportedNamedStruct,
named_struct_by_value,
named_struct_by_shared_ref,
named_struct_by_exclusive_ref,
return_named_struct,
named_struct_by_optional_value,
return_optional_named_struct,
ExportedTupleStruct,
return_tuple_struct
} from './guide_supported_types_examples';
let namedStruct = return_named_struct(42);
console.log(namedStruct instanceof ExportedNamedStruct); // true
console.log(namedStruct.inner); // 42
named_struct_by_shared_ref(namedStruct);
named_struct_by_exclusive_ref(namedStruct);
named_struct_by_value(namedStruct);
// `namedStruct` has been moved into Rust and can no longer be used.
let optionalNamedStruct = return_optional_named_struct(42);
named_struct_by_optional_value(optionalNamedStruct);
let tupleStruct = return_tuple_struct(10, 20);
console.log(tupleStruct instanceof ExportedTupleStruct); // true
console.log(tupleStruct[0], tupleStruct[1]); // 10, 20