Skip to content

Commit

Permalink
added auto un-boxing
Browse files Browse the repository at this point in the history
  • Loading branch information
maccesch committed Jul 12, 2023
1 parent 0c8b604 commit 35f704a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,57 @@ struct SomeOptions<T> {

this will generate a standard builder method as if `T` wasn't generic.

#### `Box`

The macro detects if a field is a `Box` and generates a builder method that
accepts the inner type (without `Box`) and adds the box in the body.

In case it's a `Box<dyn Trait>` the builder method will have an argument of type
`impl Trait`.

If you want to prevent this auto un-boxing you can use the `#[builder(keep_box)]` attribute.

```rust
trait Test {}

#[derive(DefaultBuilder)]
struct SomeOptions {
the_field: Box<dyn Test + 'static>,
other_field: Box<String>,

#[builder(keep_box)]
keep: Box<String>,
}
```

This will generate the following code:

```rust
impl SomeOptions {
pub fn the_field(self, value: impl Test + 'static) -> Self {
Self {
the_field: Box::new(value),
..self
}
}

pub fn other_field(self, value: String) -> Self {
Self {
other_field: Box::new(value),
..self
}
}

pub fn keep(self, value: Box<String>) -> Self {
Self {
keep: value,
..self
}
}
}
```


### Related Work

For more general purposes please check out the much more powerful
Expand Down

0 comments on commit 35f704a

Please sign in to comment.