-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
Thanks for the report! For this you'll want to use getters and setters, and that shoul dod the trick! |
That worked! Thank you. |
Hi @garrettmaring can you share some details how exactly you solved it with getters and setters? thanks |
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 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. |
@alexcrichton would it be feasible for wasm-bindgen to generate this code if a |
It's plausible, yeah! It's something though we've avoided doing historically because a |
Now that being said, it'd be a neat feature to do something like |
Thanks @garrettmaring , it works. |
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.htmlAdditional Details
The text was updated successfully, but these errors were encountered: