Code Monkey home page Code Monkey logo

ducktor's Introduction

ducktor

ducktor is a Rust crate that allows you to create types from wasm_bindgen::JsValue using duck typing.

With ducktor, you can define a struct that specifies the fields and types that you expect from a JsValue, and then use the implement From<JsValue> for it using the #[derive(FromJsValue)] macro. This way, you can use Rust's type system and syntax to work with JavaScript objects in WebAssembly.

Why

  • Using#[wasm_bindgen] on exported types requires the input on an exported function be of the class created by wasm-bindgen which is not always possible
  • Using imported types with #[wasm_bindgen] extern "C" { ... } makes it so that the type is not creatable from Rust. This becomes a problem in shared code where you want to create instance of the type from Rust as well.

Solution

Meet ducktor! A constructor for your structs using duck typing. It allows you to create a struct, as you normally would. Then, you can use the #[derive(FromJsValue)] macro to implement From<JsValue> for your struct. This allows you to use the from method on your struct to create an instance of it from a JsValue.

Internally, it defines and imports a non-existent JavaScript type that matches the struct fields and types. The JsValue is then coerced into this type using JsValue::unchecked_ref and then the struct is created by getting the value of each field from the JS type.

Example

use ducktor::FromJsValue;

// Define a struct that represents a JavaScript object with an a field and a b field
#[derive(FromJsValue)]
struct Data {
    a: u32,
    b: String,
}

// Create a JavaScript object that conforms to the Data struct
fn roundtrip() {
    let data = js_sys::Object::new();
    js_sys::Reflect::set(&data, &"a".into(), &42.into()).unwrap();
    js_sys::Reflect::set(&data, &"b".into(), &"string".into()).unwrap();

    // Convert the JsValue to a Data using the `from_js_value` method
    let data: Data = Data::from(&data.into());
    assert_eq!(data.a, 42);
    assert_eq!(data.b, "string");
}

ducktor's People

Contributors

ranile avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

dmgolembiowski

ducktor's Issues

Generate typings for types using ducktor

One of the important pieces that wasm-bindgen provides is the generated typescript declaration file that describes the public API of the generated bindings. If I understand correctly,when using ducktor in a public facing API, the types will just show up as JsValues and all type information will be lost. So even though you can pass any object with the same fields, you would not get typechecking for those objects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.