#builder-pattern #macro #generate #structs #procedural #derive-builder #proc-macro

macro builder-rs

A procedural macro for generating builder patterns for Rust structs

1 unstable release

0.1.0 Mar 16, 2024

#1197 in Procedural macros

Custom license

5KB
76 lines

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
}

Dependencies

~230–670KB
~16K SLoC