-
-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Labels
feature requestA new feature is requestedA new feature is requested
Description
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 thatCloneis 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!
Metadata
Metadata
Assignees
Labels
feature requestA new feature is requestedA new feature is requested