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

use bon instead of typed-builder #1289

Closed
rnbguy opened this issue Jul 29, 2024 · 0 comments · Fixed by #1326
Closed

use bon instead of typed-builder #1289

rnbguy opened this issue Jul 29, 2024 · 0 comments · Fixed by #1326
Labels
O: code-hygiene Objective: aims to improve code hygiene O: testing Objective: aims to improve testing coverage O: usability Objective: aims to enhance user experience (UX) and streamline product usability

Comments

@rnbguy
Copy link
Collaborator

rnbguy commented Jul 29, 2024

Improvement Summary

Rust functions don't have named arguments, and each argument is required.

fn foo(x: u64, y: bool) { ... }

But this is a bit annoying when we are writing tests with default values or fixtures. To have optional named arguments in Rust, we need to create an argument struct and pass it.

fn foo(FooArgs { x: u64, y: bool }) { ... }

Untill now, we didn't have any proper macro library that avoid this code bloat. Currently, in our tests, (mainly in ibc-testkit) we manually define these structs and auto-derive a Builder pattern using typed-builder.

#[typed_builder::TypedBuilder]
struct FooArgs { x: u64, y: bool }

foo(FooArgs::builder().x(x).y(y).build())

Proposal

Recently, there is a new library called bon which offers solutions to this exact problem.

#[bon::builder]
fn foo(x: u64, y: bool) { ... }

foo().x(x).y(y).call()

bon checks arguments at compile time, just like typed-builder.

@rnbguy rnbguy added O: testing Objective: aims to improve testing coverage O: code-hygiene Objective: aims to improve code hygiene O: usability Objective: aims to enhance user experience (UX) and streamline product usability labels Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O: code-hygiene Objective: aims to improve code hygiene O: testing Objective: aims to improve testing coverage O: usability Objective: aims to enhance user experience (UX) and streamline product usability
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant