Blog post

Builders in Rust

2022-06-09T15:00:00

β€’

17 minute read

This blog post is powered by shuttle! The serverless platform built for Rust.

In this post, we'll be going over the "builder pattern". The builder pattern is an API design pattern for constructing instances of Rust structures. We'll be going over where it makes sense to use it and some of the benefits of applying it to your structs.

Examples

Here are some examples of the builder pattern in common Rust crates:

Command from the Rust standard library

1Command::new("cmd")
2    .args(["/C", "echo hello"])
3    .output()
4

Rocket in Rocket

1rocket::build()
2    .mount("/hello", routes![world])
3    .launch()
4

Response in the HTTP crate

1Response::builder()
2    .status(200)
3    .header("X-Custom-Foo", "Bar")
4    .header("Set-Cookie", "key=2")
5    .body(())
6    .unwrap();
7

Cargo uses the pattern internally for tests

Ok - so let's dive into what the builder pattern actually is.

What is the builder pattern

Given the following struct representation:

1struct Message {
2    from: String,
3    content: String,
4    attachment: Option<String>
5}
6

Using struct initialization syntax:

1Message {
2    from: "John Smith".into(),
3    content: "Hello!".into(),
4    attachment: None
5}
6

Using a builder pattern:

1Message::builder()
2    .from("John Smith".into())
3    .content("Hello!".into())
4    .build()
5

The builder pattern consists of:

  • A function that generates a intermediate builder structure (Message::builder())
  • A chain of methods which set values on the builder: (.from("John Smith".into()).content("Hello!".into()))
  • A final method which builds the final value from the intermediate structure .build()

The structure of the builder pattern follows the functional programming design and has likeness of building iterators.

The setting methods take a mutable reference to the builder and return the same reference (thus for chaining to work). The handy part about working with mutable references is that it can be shared around between functions and if statements:

1fn build_message_from_console_input(
2    builder: &mut MessageBuilder
3) -> Result<(), Box<dyn Error>> {
4    let mut buffer = String::new();
5    let mut stdin = std::io::stdin();
6    stdin.read_line(&mut buffer).unwrap();
7    
8    let split = buffer.rsplit_once("with attachment: ");
9    if let Some((message, attachment_path)) = split {
10        let attachment = 
11            std::fs::read_to_string(attachment_path).unwrap();
12        builder
13            .content(message.into());
14            .attachment(attachment);
15    } else {
16        builder.text_filter(buffer);
17    }
18}
19

Next we'll explore some places where the builder pattern can offer a lot of benefits.

Constraints and computed data

Given the following struct which represents running a certain function at a certain time:

1struct FutureRequest<T: FnOnce()> {
2    at: chrono::DateTime<chrono::Utc>,
3    func: T
4}
5

We don't want the program to be able to create a FutureRequest for a time in the past.

With regular struct initialisation and public fields there isn't a good way to constrain the values being given to the struct1

1let fq = FutureRequest {
2    at: chrono::DateTime::from_utc(
3        chrono::NaiveDate::from_ymd(-112, 2, 18)
4            .and_hms(11, 5, 6), 
5        Utc
6    ),
7    func: || println!("π“…₯𓃢𓀫"),
8}
9

However with the builder pattern and a method for setting the time we can validate the value before it is assigned

1#[derive(Debug)]
2struct SchedulingInPastError;
3
4impl<T: FnOnce() -> ()> FutureRequestBuilder<T> {
5    fn at(
6        &mut self, 
7        date_time: chrono::DateTime<Utc>
8    ) -> Result<&mut Self, SchedulingInPastError> {
9        if date_time < Utc::now() {
10            Err(SchedulingInPastError)
11        } else {
12            self.at = date_time;
13            Ok(self)
14        }
15    }
16}
17

Maybe we don't even want an absolute time - but a relative time at some point in the future.

1impl<T: FnOnce() -> ()> FutureRequestBuilder<T> {
2    fn after(&mut self, duration: std::time::Duration) -> &mut Self {
3        self.at = Utc::now() + chrono::Duration::from_std(duration).unwrap();
4        self
5    }
6}
7

Encapsulating data

Sometimes - we want to keep some fields hidden from the user:

1struct Query {
2    pub on_database: String,
3    // ...
4}
5
6fn foo(query: &mut Query) {
7    // You want mutable access to call mutable methods on the query 
8    // but want to prevent against:
9    query.on_database.drain(..);
10}
11

So you could make the fields private and create a function which constructs the value (known as a constructor):

1impl Query {
2    fn new(
3        fields: Vec<String>,
4        text_filter: String,
5        database: String,
6        table: String,
7        fixed_amount: Option<usize>,
8        descending: bool,
9    ) -> Self {
10        unimplemented!()
11    }
12}
13
14let query = Query::new(
15    vec!["title".into()],
16    "Morbius 2".into(),
17    "imdb".into(),
18    "films".into(),
19    None,
20    false
21);
22

But this causes confusion at the call site. Its not clear whether "imdb" is the database, the table or the text_filter? 2.

The builder pattern makes it much easier to read and understand what's happening during initialisation:

1let query = Query::builder()
2    .fields(vec!["title".into()]),
3    .text_filter("Morbius 2".into()),
4    .database("imdb".into()),
5    .table("films".into()),
6    .fixed_amount(None),
7    .descending(false)
8    .build();
9

Enums and nested data

So far we've just discussed structs - let's talk about enums:

1enum HTMLNode {
2    Text(String),
3    Comment(String),
4    Element(HTMLElement)
5}
6
7struct HTMLElement {
8    tag_name: String,
9    attributes: HashMap<String, Option<String>>,
10    children: Vec<HTMLNode>
11}
12

Here there is builder associated with each variant:

1HTMLNode::text_builder()
2    .text("Some text".into())
3    .build()
4    
5// vs
6
7HTMLNode::Text("Some text".into())
8
9// --
10
11HTMLNode::element_builder()
12    .tag_name("p".into())
13    .attribute("class".into(), "big quote".into())
14    .attribute("tabindex".into(), "5".into())
15    .content("Some text")
16    
17// vs
18
19HTMLNode::Element(HTMLElement {
20    tag_name: "p".into(),
21    attributes: [
22        ("class".into(), "big quote".into()),
23        ("tabindex".into(), "5".into())
24    ].into_iter(),
25    children: vec![HTMLNode::Text("Some text".into())]
26})
27

Building our own builder pattern

Now let's build our own builders (no pun intended). In this example we have some users:

1#[derive(Debug)]
2struct User {
3    username: String,
4    birthday: NaiveDate,
5}
6
7struct UserBuilder {
8    username: Option<String>,
9    birthday: Option<NaiveDate>,
10}
11
12#[derive(Debug)]
13struct InvalidUsername;
14
15#[derive(Debug)]
16enum IncompleteUserBuild {
17    NoUsername,
18    NoCreatedOn,
19}
20
21impl UserBuilder {
22    fn new() -> Self {
23        Self {
24            username: None,
25            birthday: None,
26        }
27    }
28
29    fn set_username(&mut self, username: String) -> Result<&mut Self, InvalidUsername> {
30        // true if every character is number of lowercase letter in English alphabet
31        let valid = username
32            .chars()
33            .all(|chr| matches!(chr, 'a'..='z' | '0'..='9'));
34
35        if valid {
36            self.username = Some(username);
37            Ok(self)
38        } else {
39            Err(InvalidUsername)
40        }
41    }
42
43    fn set_birthday(&mut self, date: NaiveDate) -> &mut Self {
44        self.birthday = Some(date);
45        self
46    }
47
48    fn build(&self) -> Result<User, IncompleteUserBuild> {
49        if let Some(username) = self.username.clone() {
50            if let Some(birthday) = self.birthday.clone() {
51                Ok(User { username, birthday })
52            } else {
53                Err(IncompleteUserBuild::NoCreatedOn)
54            }
55        } else {
56            Err(IncompleteUserBuild::NoUsername)
57        }
58    }
59}
60

Some things to look out for:

  • Every set method must take a mutable reference in order to add the data to the backer
  • The method must then return the mutable reference it has to allow for them to be chained.

There are clones in the build method but if that method is only called once then it is optimized out by Rust.

Automatic approaches

Similar to how Clone and Debug work, crates can create there own derive macros. There are a lot of crates which can help with generating the builder pattern. Let's take a look at a few:

derive_builder

1#[derive(Debug, derive_builder::Builder)]
2#[builder(build_fn(validate = "Self::validate"))]
3struct Query {
4    fields: Vec<String>,
5    text_filter: String,
6    database: String,
7    table: String,
8    fixed_amount: Option<usize>,
9    descending: bool,
10}
11
12// Usage same as described patterns:
13let query = Query::builder()
14    .table("...".into())
15    // ...
16    .build()
17    .unwrap();
18

This derive macro generates a new struct named the same as the original structure but postfixed with Builder (in this case QueryBuilder).

Derive builder has the downside of a whole object validation rather than per field. As well as the error variant of construction being a String, which makes it harder to match on the error or return error data compared to a error enum:

1impl Query {
2    fn validate(&self) -> Result<(), String> {
3        let valid = self
4            .database
5            .as_ref()
6            .map(|value| value == "pg_roles")
7            .unwrap_or_default();
8
9        if valid {
10            Ok(())
11        } else {
12            Err("Cannot construct Query on 'pg_roles'".into())
13        }
14    }
15}
16

typed-builder

Typed-builder solves two problems with derive_builder:

With derive_builder you can set a field twice (or more)

1Query::builder()
2    .database("imdb".into())
3    // ...
4    .database("fishbase".into())
5

Which takes the value of the last set field which is likely a mistake. Although Rust can optimize out a write without a read it is very difficult to have a linter error for this mistake. derive_builder also delegates the check to whether all the required fields have been set to runtime.

With typed-builder it has a very similar implementation but has a different output which Rust can reason about and check that they are no duplicate sets and the build is well formed (all the required fields have been set).

The downside here is that it takes longer to expand the macros as there is more to generate. The added complexity also makes it more complicated to pass the builder around.

Buildstructor

Buildstructor is a annotation for an existing impl block. Rather than using the fields on a structure (as seen in the previous two) to generate code it builds wrappers around existing constructor functions:

1struct MyStruct {
2    sum: usize
3}
4
5#[buildstructor::buildstructor]
6impl MyStruct {
7    #[builder]
8    fn new(a: usize, b: usize) -> MyStruct {
9        Self { sum: a + b }
10    }
11}
12
13MyStruct::builder().a(1).b(2).build();
14

Similar to typed-builder it generates intermediate staging structs for building which has the benefits of compile time checking that all the fields exist. However that comes again with the drawback of slower compile time and less flexibility when passing it around.

Typed builder looks to be more compatible with the Rust language which allows it to support async builders! It's definitely the more interesting one of the bunch and I will be looking to play with with it future projects.

Alternative patterns

If you just want to build a struct which has a large amount of default fields, using .. (base syntax) with the Default trait (whether a custom implementation or the default one with #[derive(Default)]) will do:

1#[derive(Default)]
2struct X {
3    a: u32,
4    b: i32,
5    c: bool,
6}
7
8X { a: 10, ..Default::default() }
9

If you want computation, constraints, encapsulation and named fields you could create a intermediate struct which can be passed to a constructor:

1struct Report {
2    title: String,
3    on: chrono::DateTime
4    // ...
5}
6
7struct ReportArguments {
8    title: String,
9    on: Option<chrono::DateTime>
10    // ...
11}
12
13impl Report {
14    fn new_from_arguments(ReportArguments { title, on }: ReportArguments) -> Result<Self, &str> {
15        if title.
16            .chars()
17            .all(|chr| matches!(chr, 'a'..='z' | '0'..='9'))
18        {
19            Ok(Self {
20                title,
21                on: chrono.unwrap_or_else(|| todo!())
22            })
23        } else {
24            Err("Invalid report name")
25        }
26    }
27}
28

However both of these don't the use the nice chaining syntax.

Conclusion

The builder pattern can help you write cleaner, more readable APIs, and it turn help the consumers of your APIs write better code. We can apply constraints to make sure that our structs are initialised correctly with a clean API enforcing the contract.

One thing to remember is that code is read much more than it's written - so it's worth going out of our way to make our code just that little bit more pleasant to read.

Shuttle: Stateful Serverless for Rust

Deploying and managing your Rust web apps can be an expensive, anxious and time consuming process.

If you want a batteries included and ops-free experience, try out Shuttle.


Footnotes

  1. I partially agree with this, there are ways to design your types to be constrained. Here we could create a struct FutureEvent(chrono::DateTime) structure where the constraint is constructing the FutureEvent type rather than leaving the constraint to the field. But there are lots of scenarios where that isn't the case. ↩

  2. With vscode and rust analyzer there is a feature called inlay hints which shows the names of parameters in the editor. While this is great this is a feature specific to vscode at the moment. You won't see the hints on GitHub diffs and in other text editors. ↩

Share this article

Let's make Rust the next language of cloud-native

We love you Go, but Rust is just better.