bon icon indicating copy to clipboard operation
bon copied to clipboard

Support shared `#[builder]` configuration in `#[bon]` macro at the `impl` block level

Open Veetaha opened this issue 1 year ago • 0 comments

In some cases, developers would like to share the #[builder(...)] configuration across all their methods in the impl block, or even across all impl blocks.

Today, the users of bon need to copy-paste the same builder configs across all methods inside of the impl block manually, which isn't convenient when the number of methods is big and growing.

The proposed syntax is the following:

#[bon(builder(on(...), finish_fn = ..., derive(...))]
impl Foo {
    #[builder] // will inherit configs from `#[bon(builder(...))]`
    fn method(/**/) { /**/ }
    
    // Won't have a `#[builder]` generated
    fn regular_method(/**/) { /**/ }
}

This way the developers can move their configs to the top of the impl block to prevent code duplication at the entire impl block level. If the developers want to share these configs between multiple impl blocks they can easily create an attribute alias with macro_rules_attribute::attribute_alias.

Once we have this feature, the documentation for the "Shared Configuration" pattern needs to be updated to propose a solution for sharing configs for associated methods using the new syntax.

A note for the community from the maintainers

Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.

Veetaha avatar Sep 18 '24 20:09 Veetaha