-
Notifications
You must be signed in to change notification settings - Fork 1.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
Vectors not working in structs #439
Comments
Thanks for the report! Currently though public struct fields are a special case in that they only work with |
Thanks; removing pub on the field fixed it |
Related Q: How do you extract a value from a struct passed via bindgen to JS? The object I see when printing to console.log() contains a ptr field of a large integer, and a very deep recursive structure containing values like , free, constructor, bind, call etc. Eg, how can I make This page in the official guide shows example struct passing from Rust to JS, but doesn't show accessing a value contained in one. edit: Getter functions, like in the example on that guide appear to work. Is there a way around this? Perhaps auto-generating getter funcs for all struct fields? |
@David-OConnor ah that's got its own separate bug for defining JS getters in Rust |
@alexcrichton Perhaps the solution, assuming there's an obstacle to exposing fields directly, is to serialize the struct, pass it to JS, then deserialize into an object. This assumes no methods. |
Oh currently the main obstacle in a sense is that it's not clear what to do if the field isn't |
That makes sense. One way to handle this is to send one-way structs to JS, that don't call back to the Rust code. I got it working like this, using serde: Rust:
JS:
No methods, but can be fed into a constructor for a JS object that has methods, or just used as-is, optionally by declaring it with a Typescript interface. @alexcrichton Do you think this would be useful in a more streamlined, official way, eg that handles the serialization/deserialization automatically? This seems like it could be a common use-case. Eg make Rust code that does something complicated or interesting, then call a Rust func from JS, which passes the result in whichever data structures are convenient... At least when I heard of WASM and bindgen, this was the first use-case that came to mind. |
@David-OConnor oh we already have those apis actually! Lines 153 to 206 in 1a84901
|
@alexcrichton Sick! |
You also cannot store a |
This ByteString struct can't be returned from a function because the Vec is not Copy: #[wasm_bindgen]
pub struct ByteString {
pub bytes: Vec<u8>,
} But a clone directly in a getter works: #[wasm_bindgen]
impl ByteString {
pub fn get(&self) -> Vec<u8> {
self.bytes.clone()
}
} This adds a seemingly useless indrection - now every Vec that is returned anywhere needs to be wrapped in such a struct and accessed via a getter. Is there a reason why this could not work directly? Would be very appreciated. Same for String. |
This would require some design work and figuring out what to do here. |
@daxpedda Would generating getters for all struct members as @David-OConnor suggested be a good initial compromise (usability for perf) or were you thinking on other (better) design patterns from the get-go? I'm willing to give either a try after fixing my cargo issues with wasm-bindgen. |
It would have to be done with the help of an attribute, because doing it by default is probably a bad idea. Personally I'm not much in favor of doing this, as this can be done outside of |
I'm receiving the following error:
the trait
std::marker::Copyis not implemented for
std::vec::Vecfor this struct:
If I change tris' type to u32, the error goes away.
This page lists Vectors and slices of supported integer types as supported.
The text was updated successfully, but these errors were encountered: