Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bare objects to be passed to functions taking exported types #3493

Closed
hamza1311 opened this issue Jun 21, 2023 · 3 comments
Closed

Allow bare objects to be passed to functions taking exported types #3493

hamza1311 opened this issue Jun 21, 2023 · 3 comments

Comments

@hamza1311
Copy link
Collaborator

Motivation

At the moment, when calling a function exported by wasm-bindgen, there's no way to convert to Rust structs without creating a class instance of the exported wasm-bindgen type.

Calling without creating a class instance is needed in cases where a class instance cannot be created, such as when interfacing with existing code is needed.

Proposed Solution

Given the following code

#[wasm_bindgen]
pub struct Data {
    key_id: String,
    name: String,
}

#[wasm_bindgen]
pub fn do_work(data: Data) -> String {
    format!(r#"
        key_id: {}
        name: {}
    "#, data.key_id, data.name)
}

The following call should be possible

do_work({
  key_id: 'key_id',
  name: 'name',
})

See #3468 (comment)

Alternatives

Use imported types with duck typing. Type imports with duck typing don't always work though. In my case, this is shared code and I need everything to also be usable from rust

@jkomyno
Copy link

jkomyno commented Jun 22, 2023

Alternative projects that integrate wasm_bindgen to support creating structs in place (i.e., what OP referred to as "duck typing") are:

@Liamolucko
Copy link
Collaborator

There's also the older issue #2645 open about this.

@daxpedda
Copy link
Collaborator

Closing this in favor of #2645 then.

@daxpedda daxpedda closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants