Skip to content

Commit 21d16a1

Browse files
committed
Improve the guide section
1 parent ea97416 commit 21d16a1

File tree

4 files changed

+59
-35
lines changed

4 files changed

+59
-35
lines changed

website/.vitepress/config.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ export default defineConfig({
160160
link: "/guide/basics/positional-members",
161161
},
162162
{
163-
text: "Inspecting",
164-
link: "/guide/basics/inspecting",
163+
text: "Derives for Builders",
164+
link: "/guide/basics/derives-for-builders",
165165
},
166166
{
167167
text: "Documenting",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Derives for Builders
2+
3+
You can specify some extra derives on the generated builder struct itself via the top-level [`#[builder(derive(...))]`](../../reference/builder/top-level/derive) attribute.
4+
5+
For example, if you want to inspect the values set in the builder for debugging purposes you can derive the `Debug` trait for your builder.
6+
7+
```rust
8+
use bon::builder;
9+
10+
#[builder(derive(Debug))] // [!code highlight]
11+
fn example(
12+
name: String,
13+
is_admin: bool,
14+
level: Option<u32>,
15+
) {}
16+
17+
let builder = example().name("Bon".to_owned());
18+
19+
// This will output the current state of the builder to `stderr`
20+
dbg!(&builder);
21+
22+
// You can also format the debug output to `String`:
23+
assert_eq!(
24+
format!("{builder:?}"),
25+
// Only the fields that were set will be output
26+
r#"ExampleBuilder { name: "Bon" }"#
27+
);
28+
29+
// Finish building
30+
builder.is_admin(true).call();
31+
```
32+
33+
You can also derive the `Clone` and `Into` traits for your builder using this same attribute. See more details in the [reference for the `#[builder(derive(...))]` attribute](../../reference/builder/top-level/derive).

website/src/guide/basics/inspecting.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

website/src/reference/builder/top-level/derive.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ assert_eq!(
137137

138138
:::
139139

140-
## `Clone` and `Debug` derives
140+
## `Clone` and `Debug` Derives
141141

142-
### Generic types handling
142+
### Generic Types Handling
143143

144144
If the underlying `struct` or `fn` contains generic type parameters, then the generated impl block will include a `where` bound requiring the respective trait to be implemented by all of them. This follows the behaviour of the [standard `derive` macros](https://doc.rust-lang.org/std/clone/trait.Clone.html#derivable).
145145

@@ -248,3 +248,25 @@ Note that `#[builder(derive(Into))]` is quite limited. Here are some things it d
248248
- `async` functions, because `From::from()` is synchronous
249249
- `unsafe` functions, because `From::from()` is safe
250250
- Members marked with [`#[builder(finish_fn)]`](../member/finish_fn) because `From::from()` doesn't accept arguments
251+
252+
### Use Cases
253+
254+
This derive is most useful to reduce the boilerplate of calling the finishing function when the result of building is passed to some other function that accepts `impl Into<T>`.
255+
256+
```rust
257+
use bon::Builder;
258+
259+
#[derive(Builder)]
260+
#[builder(derive(Into))]
261+
struct Example {
262+
x1: u32,
263+
}
264+
265+
fn take_example(value: impl Into<Example>) { /* */ }
266+
267+
take_example(
268+
// You can omit the call to `build()` here
269+
Example::builder()
270+
.x1(99)
271+
)
272+
```

0 commit comments

Comments
 (0)