You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/src/reference/builder/top-level/derive.md
+67-15Lines changed: 67 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ _⚠️ Do not confuse this with `#[derive(bon::Builder)]`⚠️_
6
6
7
7
Generates additional derives for the builder struct itself. The syntax is similar to the regular `#[derive(...)]` attribute, but it must be wrapped in `#[builder(derive(...))]`. Expects one or more of the supported derives separated by a comma.
8
8
9
-
The following derives are supported: `Clone`, `Debug`.
9
+
The following derives are supported: [`Clone`, `Debug`](#clone-and-debug-derives), [`Into`](#into-derive).
10
10
11
11
::: warning
12
12
The format of the `Debug` output of the builder is not stable, and it may change between patch versions of `bon`.
@@ -20,7 +20,7 @@ The format of the `Debug` output of the builder is not stable, and it may change
// We can use `From<ExampleBuilder>` to `Example` instead of
125
+
// calling .build()
126
+
.into();
127
+
128
+
assert_eq!(example, 99);
111
129
112
130
// The debug output of the builder for methods with `self` includes
113
131
// the special `self` field with the receiver.
@@ -119,9 +137,11 @@ assert_eq!(
119
137
120
138
:::
121
139
122
-
## Generic types handling
140
+
## `Clone` and `Debug` derives
141
+
142
+
### Generic types handling
123
143
124
-
If the underlying `struct` or `fn` contains generic type parameters, then the generated impl block will include a `where` bound requiring the respective trait (`Clone` or `Debug`) 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).
144
+
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).
125
145
126
146
This works fine in most cases, but sometimes the generated bounds may be overly restrictive. To fix that, you can manually specify the bounds using the syntax `#[builder(derive(Trait(bounds(...))))]`, where `...` is a comma-separated list of `where` bounds.
127
147
@@ -177,7 +197,7 @@ If you'd like to know why this attribute is this dumb and doesn't just add a `wh
177
197
178
198
:::
179
199
180
-
## Compile errors
200
+
###Compile errors
181
201
182
202
_Requires_ that all members of the builder including the receiver (if this is a builder for an associated method) implement the target trait. For example, this doesn't compile because not all members implement `Clone`:
183
203
@@ -196,3 +216,35 @@ struct Example {
196
216
cloneable:u32
197
217
}
198
218
```
219
+
220
+
## `Into` derive
221
+
222
+
Somewhat obviously, but `Into` derive actually generates a `From` implementation, providing the `Into` trait implementation automatically via the [blanket `impl` in std](https://doc.rust-lang.org/stable/std/convert/trait.From.html#generic-implementations).
223
+
224
+
::: details See an example of what code it generates:
225
+
226
+
(you can even write it manually actually)
227
+
228
+
```rust
229
+
usebon::Builder;
230
+
231
+
#[derive(Builder)]
232
+
structExample {
233
+
x1:String,
234
+
}
235
+
236
+
// This is what #[builder(derive(Into))] would generate:
0 commit comments