Skip to content

Feature Request: method to fill the remaining values from an instance of existing built object #310

@BernardIgiri

Description

@BernardIgiri

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions