Expand description
Provides utilities, helpers and macros to easily build and customize wasm_bindgen bindings,
such as impl_wasm_traits macro that will implement wasm traits for a give type and
serialize_hashmap_as_object serializer function to serialize a hashmap as object used with
serde serialize_with attribute.
For more details please read the doumentation of the items of this lib.
Example:
ⓘ
use wasm_bindgen_utils::{prelude::*, impl_wasm_traits, impl_custom_tsify, add_ts_content};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SomeType {
    #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_as_bytes"))]
    pub field: Vec<u8>,
    #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_hashmap_as_object"))]
    pub other_field: HashMap<String, u8>,
}
// impl wasm traits for SomeType
impl_wasm_traits!(SomeType);
// impl tsify manually for SomeType (as an alternative to Tsify derive macro)
// the given string literal will become the typescript interface bindings for SomeType
impl_custom_tsify!(
    SomeType,
    "export interface SomeType {
        field: Uint8Array;
        otherField: Record<string, number>;
    }"
);
// appends a custom section to the .d.ts generated bindings
add_ts_content!("import { Something } from 'some-js-lib'")
// now someType can be used on functions and methods natively
#[wasm_bindgen]
pub fn some_fn(arg: SomeType) -> String {
    // body
}
#[wasm_bindgen]
pub async fn some_other_fn(arg1: Vec<u8>, arg2: HashMap<String, u8>) -> Result<SomeType, Error> {
    // body
    Ok(SomeType {
        field: arg1,
        other_field: arg2
    })
}Modules§
Macros§
- add_ts_ content 
- Adds/appends the given string literal to wasm bindgen typescript bindings.
This is just a sugar for wasm_bindgen typescript_custom_section, so the given text can be anything, from typescript comment to type declarations or any other valid .d.ts content.
- impl_complementary_ wasm_ traits 
- Implements complementary wasm traits for the given type. Needs impl_main_wasm_traits to be implemented first. It allows a type to be used on async functions normally or as ref or as Vec<> etc. The type needs to have serde::Serialize, serde::Deserialize and tsify::Tsify traits implemented.
- impl_custom_ tsify 
- Implements tsify::Tsify with the given type declaration for the given rust type (structs and enums) identifier.
- impl_main_ wasm_ traits 
- A macro that implements main wasm traits for the given type. These traits are the necessary ones to be able to send/receive the given type through wasm bindgen boundry. The type needs to have serde::Serialize, serde::Deserialize and tsify::Tsify traits implemented.
- impl_wasm_ traits 
- Implement all wasm traits for the given type. that is impl_main_wasm_traits and impl_complementary_wasm_traits. The type needs to have serde::Serialize, serde::Deserialize and tsify::Tsify traits implemented.
Functions§
- serialize_as_ bytes 
- Serializer fn for serializing Vec<u8> as bytes (Uint8Array for js) Example:
- serialize_btreemap_ as_ object 
- Serializer fn that serializes BTreeMap as k/v object. in js it would be plain js object and not js Map.
- serialize_hashmap_ as_ object 
- Serializer fn that serializes HashMap as k/v object. in js it would be plain js object and not js Map.
- serialize_i64_ as_ bigint 
- Serializer fn for serializing i64 as js bigint Example:
- serialize_opt_ btreemap_ as_ object 
- Same as serialize_btreemap_as_object but for Option<BTreeMap>
- serialize_opt_ hashmap_ as_ object 
- Same as serialize_hashmap_as_object but for Option<HashMap>
- serialize_u64_ as_ bigint 
- Serializer fn for serializing u64 as js bigint Example: