#wasm-bindgen #proc-macro #serialization #lib #wasm-bindgen-utils #arg1 #arg2

macro wasm-bindgen-utils-macros

Provides helper proc macros for wasm-bindgen-utils

4 releases

0.0.5 Jun 7, 2025
0.0.4 Jun 4, 2025
0.0.3 May 9, 2025
0.0.2 Mar 25, 2025

#2827 in Procedural macros

Download history 1255/week @ 2025-06-30 1191/week @ 2025-07-07 1065/week @ 2025-07-14 1285/week @ 2025-07-21 436/week @ 2025-07-28 243/week @ 2025-08-04 543/week @ 2025-08-11 380/week @ 2025-08-18 321/week @ 2025-08-25 472/week @ 2025-09-01 363/week @ 2025-09-08 227/week @ 2025-09-15 256/week @ 2025-09-22 512/week @ 2025-09-29 1143/week @ 2025-10-06 316/week @ 2025-10-13

2,230 downloads per month
Used in 2 crates (via wasm-bindgen-utils)

LicenseRef-DCL-1.0

69KB
1.5K SLoC

wasm-bindgen-utils

Provides utilities, helpers and macros to easily build and customize wasm_bindgen bindings. 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
    })
}

Dependencies

~205–650KB
~15K SLoC