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

How do you use a Rust struct with a String field? #1775

Closed
garrettmaring opened this issue Sep 19, 2019 · 8 comments
Closed

How do you use a Rust struct with a String field? #1775

garrettmaring opened this issue Sep 19, 2019 · 8 comments
Labels

Comments

@garrettmaring
Copy link

garrettmaring commented Sep 19, 2019

Summary

How do you use a Rust struct with a String field using wasm-bindgen?

The String type seems to be supported for function parameters and return values. https://rustwasm.github.io/docs/wasm-bindgen/reference/types/string.html

Additional Details

#[wasm_bindgen]
pub struct Data {
    id: String,
}
@alexcrichton
Copy link
Contributor

Thanks for the report! For this you'll want to use getters and setters, and that shoul dod the trick!

@garrettmaring
Copy link
Author

That worked! Thank you.

@andimarek
Copy link

Hi @garrettmaring can you share some details how exactly you solved it with getters and setters? thanks

@garrettmaring
Copy link
Author

Sure!

// doesn't work...
#[wasm_bindgen]
struct Data {
    pub id: String,
}

You'll get the error error[E0277]: the trait bound std::string::String: std::marker::Copy is not satisfied.

Since, the String type in Rust isn't implicitly copyable. I had to read up on the difference between Copy and Clone to understand that I couldn't just implement Copy but rather needed to use .clone() to explicitly copy it.

Thankfully, wasm-bindgen gives us a simple way to do it.

#[wasm_bindgen]
struct Data {
    id: String, // ensure that the field is private
}

#[wasm_bindgen]
impl Data {
    #[wasm_bindgen(getter)]
    pub fn id(&self) -> String {
        self.id.clone()
    }

    #[wasm_bindgen(setter)]
    pub fn set_id(&mut self, id: String) {
        self.id = id;
    }
}

There are some interesting things that you can do with getters and setters that are documented here.

@garrettmaring
Copy link
Author

@alexcrichton would it be feasible for wasm-bindgen to generate this code if a struct implements Clone?

@alexcrichton
Copy link
Contributor

It's plausible, yeah! It's something though we've avoided doing historically because a Clone implementation can often be accidentally quite expensive, so we tend to prefer to request that users do so manually to ensure they know the cost they're opt-ing into

@alexcrichton
Copy link
Contributor

Now that being said, 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

@terwer
Copy link

terwer commented Nov 10, 2023

Thanks @garrettmaring , it works.

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

No branches or pull requests

4 participants