Skip to content

Add #[wasm_bindgen(getter_with_clone)] attribute #2633

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

Merged
merged 3 commits into from
Jul 29, 2021

Conversation

trevyn
Copy link
Contributor

@trevyn trevyn commented Jul 26, 2021

#1775 (comment) :

it'd be a neat feature to do something like #[wasm_bindgen(getter_setter_with_clone)] or something like that so the boilerplate could be drastically reduced

// doesn't work: "the trait bound std::string::String: std::marker::Copy is not satisfied"
#[wasm_bindgen]
struct Data {
    pub id: String,
}

// now does work
#[wasm_bindgen(getter_with_clone)]
struct Data {
    pub id: String,
}

@alexcrichton
Copy link
Contributor

Thanks! Can you add both documentation and tests for this? Also I would naively expect this to be at the field level rather than the struct level?

@trevyn trevyn force-pushed the getter_with_clone branch from a570ebe to b97cdb6 Compare July 29, 2021 18:27
@trevyn
Copy link
Contributor Author

trevyn commented Jul 29, 2021

@alexcrichton Done. It works at both the field level and the struct level, the latter to reduce boilerplate in the case where it needs to be applied to many fields.

@alexcrichton
Copy link
Contributor

Thanks!

@alexcrichton alexcrichton merged commit 814efc9 into rustwasm:master Jul 29, 2021
@fosskers
Copy link

fosskers commented Jul 30, 2021

Hah, just ran into the need for this today!

Yup, works like a charm.

@brandonros
Copy link

use wasm_bindgen::prelude::*;

#[wasm_bindgen(getter_with_clone)]
pub struct Input {
    pub name: String
}

#[wasm_bindgen]
extern {
    pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(input: Input) {
    alert(&format!("Hello, {}!", input.name));
}
<!doctype html>
<html>
  <head>
  </head>
  <body>
    <script type="module">
      import * as wasm from '../pkg/wasm_driver.js'

      const run = async () => {
        await wasm.default()
        const input = new wasm.Input()
        input.name = 'James'
        wasm.greet(input)
      }

      run()
    </script>
  </body>
</html>
(index):14 Uncaught (in promise) Error: null pointer passed to rust
    at imports.wbg.__wbindgen_throw (wasm_driver.js:179:15)
    at 554b666e:0x38a8
    at 554b666e:0x3890
    at 554b666e:0x342d
    at Input.set name [as name] (wasm_driver.js:134:14)
    at run ((index):12:20)

@trevyn
Copy link
Contributor Author

trevyn commented Apr 20, 2022

@brandonros I think you have to do something like this:

#[wasm_bindgen]
impl Input {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self::default()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants