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

Feature Request: flatten for substructs #255

Closed
EvanCarroll opened this issue May 22, 2022 · 1 comment
Closed

Feature Request: flatten for substructs #255

EvanCarroll opened this issue May 22, 2022 · 1 comment

Comments

@EvanCarroll
Copy link

EvanCarroll commented May 22, 2022

It would be nice to be able to flatten structs that also implement builder.

#[derive(builder)]
struct Foo {
  bar: u32
}

#[derive(builder)]
struct Baz {
  #[builder(flatten)]
  foo: Foo // which itself derives Builder
}

// .bar() here comes from flattening `foo`
let baz = BazBuilder::from().bar(42).build().unwrap();
@TedDriggs
Copy link
Collaborator

I don't think this is possible: It would require the parent to know the methods of the child, and Rust macros don't have a way to access that data.

serde is able to support flattening because it doesn't generate methods per-field, so it can wait until runtime and then pass values down to the flattened Deserializer instances. It's very elegant, but it also doesn't work for our use-case.

@ijackson explored nested builders in #254, but that wasn't quite flattening.

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

No branches or pull requests

2 participants