Skip to content

Commit 4cd54d2

Browse files
committed
Add comment on the types of extensible builders
1 parent c77f81a commit 4cd54d2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

content/blog/2025-10-12-v0.5.0-release.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ pub struct FooBar {
175175
You can now construct a `FooBar` instance while letting the `bar` field fall back to its default value of `0`:
176176

177177
```rust
178-
let foo_bar = FooBar::builder()
179-
.build_field(PhantomData::<Symbol!("foo")>, "foo".to_owned())
180-
.finalize_with_default();
178+
let foo_bar = FooBar::builder() // __PartialFooBar<IsNothing, IsNothing>
179+
.build_field(PhantomData::<Symbol!("foo")>, "foo".to_owned()) // __PartialFooBar<IsPresent, IsNothing>
180+
.finalize_with_default(); // FooBar
181181
```
182182

183183
Behind the scene, `finalize_with_default` works by using the `UpdateField` trait to perform a *natural transformation* on each field modifier. It applies the `Default` implementation to convert `IsNothing` fields into `IsPresent`, completing the record automatically.
@@ -193,11 +193,11 @@ To address this limitation, a new `IsOptional` field state has been introduced.
193193
You can create and use an optional builder with the `optional_builder`, `set`, and `finalize_optional` methods as shown below:
194194

195195
```rust
196-
let foo_bar = FooBar::optional_builder()
197-
.set(PhantomData::<Symbol!("foo")>, "foo".to_owned())
198-
.set(PhantomData::<Symbol!("bar")>, 42)
199-
.finalize_optional()
200-
.unwrap()
196+
let foo_bar = FooBar::optional_builder() // __PartialFooBar<IsOptional, IsOptional>
197+
.set(PhantomData::<Symbol!("foo")>, "foo".to_owned()) // __PartialFooBar<IsOptional, IsOptional>
198+
.set(PhantomData::<Symbol!("bar")>, 42) // __PartialFooBar<IsOptional, IsOptional>
199+
.finalize_optional() // Result<FooBar, String>
200+
.unwrap() // FooBar
201201
```
202202

203203
Unlike the original typestate builder, the type of the builder remains `__PartialFooBar<IsOptional, IsOptional>` after each call to `set`. The `finalize_optional` method returns a `Result`, producing an error if any field contains a `None` value. This check is necessary because, without the typestate guarantees, the compiler cannot ensure at compile time that all fields are initialized.

0 commit comments

Comments
 (0)