wasm_bindgen_duck_type

Crates.iowasm_bindgen_duck_type
lib.rswasm_bindgen_duck_type
version0.1.0
sourcesrc
created_at2022-10-06 02:22:20.256028
updated_at2022-10-06 02:22:20.256028
descriptionMacro to automatically generate duck type interfaces with wasm_bindgen
homepagehttps://github.com/Graicc/wasm_bindgen_duck_type
repository
max_upload_size
id680903
size6,200
Graic (Graicc)

documentation

https://docs.rs/wasm_bindgen_duck_type

README

wasm_bindgen_duck_type

Macro to automatically generate duck type interfaces with wasm_bindgen

Why

For most cases, Serde should be used to serialize and deserialize objects passed between JavaScript and Rust. Additionally, if the JavaScript object is an instance of a class, this crate is not necessary, as the implementation of the class can be used.

However, some types, such as a web_sys::Element or js_sys::Function, cannot be serialized as text, so this approach will not work. In these cases, this macro can be used to provide a typed interface for interaction, when the JavaScript object is untyped.

How

This crate generates a Duck Typed interface based on the given struct definition, as well as a new constructor and Default trait implementation.

Usage

The [wasm_bindgen_duck_type] attribute macro can be applied to structs to generate the duck type interface for the struct.

Example

JavaScript:

function MyFun() {
    return {
        number: 20,
        fun: () => {
            console.log("Hello, World!")
        }
    };
}

function GetNumber(input) {
    return input.number
}

Rust:

use wasm_bindgen_duck_type::wasm_bindgen_duck_type;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    fn MyFun() -> MyType;
    fn GetNumber(input: MyType) -> i32;
}

#[wasm_bindgen_duck_type]
struct MyType {
    number: i32,
    fun: js_sys::Function,
}

fn main() {
    let result = MyFun();

    result.fun().call0(&JsValue::NULL);
    result.set_number(10);

    let input = MyType::default(); // All fields are null / default
    let input = MyType::new(
        42,
        JsValue::NULL.into() // Pass a function
    );

    let num = GetNumber(input); // 42
}
Commit count: 0

cargo fmt