builder-rs

Crates.iobuilder-rs
lib.rsbuilder-rs
version0.1.0
sourcesrc
created_at2024-03-16 13:46:09.238565
updated_at2024-03-16 13:46:09.238565
descriptionA procedural macro for generating builder patterns for Rust structs.
homepage
repositoryhttps://github.com/yourlogarithm/builder-rs
max_upload_size
id1175641
size8,031
Vlad-Sebastian Cretu (yourlogarithm)

documentation

README

Yet Another Rust Builder Pattern Derive Macro

Usage

#[derive(Builder)]
struct Foo {
    pub a: i32,
    pub b: String,
    pub c: bool,
    pub d: Option<usize>,
}

let foo = Foo::builder()
    .a(-3)
    .b("Hello, world!".to_string())
    .c(true)
    .d(Some(2048))
    .build();

assert_eq!(-3, foo.a);
assert_eq!("Hello, world!".to_string(), foo.b);
assert_eq!(true, foo.c);
assert_eq!(Some(2048), foo.d);

Note

The macro requires that fields implement the Default. When a value for the field is not provided it will assign the default value:

#[derive(Builder)]
struct DefaultFoo {
    pub a: i32,
    pub b: bool,
}

let foo = DefaultFoo::builder()
    .build();

assert_eq!(0, foo.a);
assert_eq!(false, foo.b);

Declaring fields that do not implement the Default trait will result in a compile error:

pub struct NonDefault {}

#[derive(Builder)]
pub struct Foo {
    d: NonDefault, // error[E0277]: the trait bound `NonDefault: Default` is not satisfied
}
Commit count: 0

cargo fmt