I'd like to propose support for a .from(&T) method on bon builders that prepopulates the builder fields by cloning from an existing instance. This would streamline a common use case such as updating immutable database entities in web applications.
Use Case
This is especially useful in web servers: a struct is loaded from the database, a few fields are updated, and the new value is persisted. Currently, this requires repetitive field copying:
let updated = User::builder()
.id(user.id.clone())
.email(user.email.clone())
.username("new_username".into())
.build();
With .from(&user):
let updated = User::builder()
.from(&user)
.username("new_username".into())
.build();
Proposal
- Add
.from(&T) to the builder, opt-in via #[builder(from_instance)].
#[builder(from_instance)] can ensure that Clone is derived as well.
- Clone each field from the input value.
- Skip fields marked
#[builder(skip)].
- Maintain
bon's type-state guarantees.
Benefits
- Reduces boilerplate for partial updates.
- Aligns with real-world workflows (e.g., editing DB records).
- Keeps compile-time safety and existing behavior intact.
Let me know if this is something you'd consider. I'd be happy to assist or prototype it.
Thank you for making bon, it has saved me so many hours of writing Rust boilerplate code!
I'd like to propose support for a
.from(&T)method onbonbuilders that prepopulates the builder fields by cloning from an existing instance. This would streamline a common use case such as updating immutable database entities in web applications.Use Case
This is especially useful in web servers: a struct is loaded from the database, a few fields are updated, and the new value is persisted. Currently, this requires repetitive field copying:
With
.from(&user):Proposal
.from(&T)to the builder, opt-in via#[builder(from_instance)].#[builder(from_instance)]can ensure thatCloneis derived as well.#[builder(skip)].bon's type-state guarantees.Benefits
Let me know if this is something you'd consider. I'd be happy to assist or prototype it.
Thank you for making
bon, it has saved me so many hours of writing Rust boilerplate code!