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

Adding #[wasm_bindgen] to Builder #221

Closed
thorlucas opened this issue Nov 19, 2021 · 5 comments
Closed

Adding #[wasm_bindgen] to Builder #221

thorlucas opened this issue Nov 19, 2021 · 5 comments

Comments

@thorlucas
Copy link

Hi,

Is it possible to add this macro to the builder? Maybe just behind a feature flag.

Thanks,

@TedDriggs
Copy link
Collaborator

Is it necessary in that situation to add it to the setter methods as well?

@thorlucas
Copy link
Author

I believe it only needs to be implemented on any methods that must be available from JavaScript.

@TedDriggs
Copy link
Collaborator

On the surface this sounds possible, but I have some concerns around how it will play with other features:

  • Will it work with &mut self setters?
  • Will it work with #[builder(setter(into))] setters, which have type parameters in their arguments?
  • Will it work with the new each setters?

It's important to me that new features largely interoperate with other existing features; documentation of proc-macros is still difficult, so having too many edge cases would make the crate very hard to use.

I currently have other time commitments that prevent me from working on this, but if someone else wants to take on the implementation and testing I'll happily review the results.

@TedDriggs
Copy link
Collaborator

I don't see good answers to the above questions on the horizon, and don't think derive_builder can take on the test burden of making this fully supported.

That said, I also don't see as many Builder-style APIs in JS; much more often, I see APIs that take an Options plain JS object and use that to initialize the class. I might suggest using the builder for construction in Rust, but then having another function that is #[wasm_bindgen] that takes a JsObject and initializes your type from that.

One could even make a proc-macro for it, e.g. #[derive(OptionsConstructor)] - that would be able to handle the parsing of the object and mapping of its properties to fields on the target struct.

@TedDriggs
Copy link
Collaborator

This is now actually possible using #[builder_setter_attr(wasm_bindgen)], but you'll have to be careful not to use features that break WASM (e.g. generics, &mut self, etc.) - derive_builder will not do any testing of compatibility between the options you've chosen for your builder and additional attributes you choose to apply.

TedDriggs added a commit that referenced this issue Mar 16, 2022
Following on from the addition of attribute pass-through for fields
and setters, it's now possible to specify attributes for the struct
using `#[builder_struct_attr(...)]` and for the inherent impl block
using `#[builder_impl_attr(...)]`.

With this change, container-level `serde` attributes are supported
and `#[wasm_bindgen]` is possible, fixing #221 (though making the
builder work with WASM remains the caller's responsibility).

Fixes #239
TedDriggs added a commit that referenced this issue Mar 16, 2022
Following on from the addition of attribute pass-through for fields
and setters, it's now possible to specify attributes for the struct
using `#[builder_struct_attr(...)]` and for the inherent impl block
using `#[builder_impl_attr(...)]`.

With this change, container-level `serde` attributes are supported
and `#[wasm_bindgen]` is possible, fixing #221 (though making the
builder work with WASM remains the caller's responsibility).

Fixes #239
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

2 participants