@@ -27,29 +27,27 @@ No more worrying whether the `build` call on your builder will return `Ok` or no
27
27
``` rust
28
28
use typesafe_builders :: prelude :: * ;
29
29
30
- fn main () {
31
- #[derive(Builder )]
32
- struct Point {
33
- #[builder(constructor)]
34
- x : u8 ,
35
- y : u8 ,
36
- #[builder(optional)]
37
- z : Option <u8 >,
38
- }
30
+ #[derive(Builder )]
31
+ struct Point {
32
+ #[builder(constructor)]
33
+ x : u8 ,
34
+ y : u8 ,
35
+ #[builder(optional)]
36
+ z : Option <u8 >,
37
+ }
39
38
40
- // `builder` requires `x` since it is marked as `constructor`.
41
- let builder = Point :: builder (1 );
42
- // These do not compile:
43
- // partial.x(6); // `x` is already set
44
- // partial.build(); // `y` is not set
39
+ // `builder` requires `x` since it is marked as `constructor`.
40
+ let builder = Point :: builder (1 );
41
+ // These do not compile:
42
+ // partial.x(6); // `x` is already set
43
+ // partial.build(); // `y` is not set
45
44
46
- // `build` is only available once all required fields are set:
47
- let result = builder . y (2 ). build ();
45
+ // `build` is only available once all required fields are set:
46
+ let result = builder . y (2 ). build ();
48
47
49
- assert_eq! (result . x, 1 );
50
- assert_eq! (result . y, 2 );
51
- assert_eq! (result . z, None );
52
- }
48
+ assert_eq! (result . x, 1 );
49
+ assert_eq! (result . y, 2 );
50
+ assert_eq! (result . z, None );
53
51
```
54
52
55
53
@@ -97,12 +95,10 @@ pub struct Struct {
97
95
x : u8 ,
98
96
}
99
97
100
- fn main () {
101
- // without x
102
- Struct :: builder (). build ();
103
- // with x
104
- Struct :: builder (). x (4 ). build ();
105
- }
98
+ // without x
99
+ Struct :: builder (). build ();
100
+ // with x
101
+ Struct :: builder (). x (4 ). build ();
106
102
```
107
103
108
104
### Constructor
@@ -118,11 +114,10 @@ pub struct Struct {
118
114
x : u8 ,
119
115
}
120
116
121
- fn main () {
122
- Struct :: builder (4 ). build ();
123
- // does not work:
124
- // Struct::builder(4).x(5).build();
125
- }
117
+ Struct :: builder (4 ). build ();
118
+
119
+ // This does not compile since `x` is already set:
120
+ // Struct::builder(4).x(5).build();
126
121
```
127
122
128
123
### Decay
@@ -138,10 +133,8 @@ pub struct Struct {
138
133
x : Option <u8 >,
139
134
}
140
135
141
- fn main () {
142
- // Use `4` instead of `Some(4)`
143
- Struct :: builder (). x (4 ). build ();
144
- }
136
+ // You can use `4` now instead of `Some(4)`:
137
+ Struct :: builder (). x (4 ). build ();
145
138
```
146
139
147
140
# How does it work?
@@ -188,9 +181,7 @@ pub struct Struct<'a, 'b, 'c> {
188
181
x : & 'a Box <& 'b Option <& 'c str >>, // yikes
189
182
}
190
183
191
- fn main () {
192
- Struct :: builder (). x (& Box :: new (& Some (" hi" ))). build ();
193
- }
184
+ Struct :: builder (). x (& Box :: new (& Some (" hi" ))). build ();
194
185
```
195
186
196
187
### Generics
@@ -207,9 +198,7 @@ mod other {
207
198
}
208
199
}
209
200
210
- fn main () {
211
- other :: Struct :: <u8 >:: builder (). y (Some (4 )). build ();
212
- }
201
+ other :: Struct :: <u8 >:: builder (). y (Some (4 )). build ();
213
202
```
214
203
215
204
### Const Generics
@@ -226,9 +215,7 @@ mod other {
226
215
}
227
216
}
228
217
229
- fn main () {
230
- other :: Struct :: <1 >:: builder (). x ([1 ]). build ();
231
- }
218
+ other :: Struct :: <1 >:: builder (). x ([1 ]). build ();
232
219
```
233
220
234
221
# TODOs
0 commit comments