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

Custom struct definition #80

Closed
colin-kiegel opened this issue Apr 15, 2017 · 4 comments
Closed

Custom struct definition #80

colin-kiegel opened this issue Apr 15, 2017 · 4 comments

Comments

@colin-kiegel
Copy link
Owner

colin-kiegel commented Apr 15, 2017

derive_builder could skip the struct definition and leave that to the user. It would then only generate the setters.

Possible Use Cases

Limitations / Drawbacks

  • the user would need to exactly match the signature and error messages will most likely be not very helpful due to lacking span information, like "some type was not as expected .. ok, which one?". But on the other hand it's really just wrapping everything in an Option.
  • it would be very unusual, or even surprising, to have a #[derive(...)] struct A generate impls on a struct B which is defined manually

Example

/// Derive builder, but skip the struct definition.
#[derive(Builder)]
#[builder(custom="ConfigBuilder")]
pub struct Config {
    pub appliance: Appliance,
    pub manual_limit: Option<usize>,
}

/// Manual implementation of the builder struct with `serde` derive
#[derive(Serde)]
pub struct ConfigBuilder {
    appliance: Option<Appliance>,

    #[serde(default, rename = "limit")]
    manual_limit: Option<Option<usize>>,
}
@TedDriggs
Copy link
Collaborator

Maybe this should be a separate derive(...) trait that goes on the handcrafted Builder struct? Something like #[derive(Setter)]? The downside then is that it's weird to have the #[builder(...)] attributes take effect on something that isn't a builder.

@colin-kiegel
Copy link
Owner Author

Hm, if we put the derive on the builder struct, I'd expect that most, if not all, attributes would require a different interpretation (e.g. skip, name`). This could nearly double our amount of testcases. That would be the main problem with that IMO.

@TedDriggs
Copy link
Collaborator

Similar to @faern's comment here on #67, having to write the struct definition manually (and keep all fields and docs in sync) would lose a lot of the value of having the builder. That sort of setter generation could be done without proc macros.

To me, the benefit of proc macros is not just LOC saved, it's time saved keeping names in sync, and time saved reading code because boilerplate isn't presented.

@TedDriggs
Copy link
Collaborator

I don't see this happening; closing the issue to maintain repo hygiene.

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