Skip to content

Commit

Permalink
Fix #118: Use generics with the constructor in build method
Browse files Browse the repository at this point in the history
  • Loading branch information
idanarye committed Sep 21, 2023
1 parent a3dca20 commit 57b6256
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed
- Use generics with the constructor in `build` method (see issue #118)

## 0.16.1 - 2023-09-18
### Fixed
- Add `#[allow(clippy::exhaustive_enums)]` to generated empty enums used for
Expand Down
21 changes: 21 additions & 0 deletions tests/tests.rs
Expand Up @@ -779,3 +779,24 @@ fn test_prefix_and_suffix() {
let foo = Foo::builder().with_x_value(1).with_y_value(2).build();

Check warning on line 779 in tests/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

use of a disallowed/placeholder name `foo`

warning: use of a disallowed/placeholder name `foo` --> tests/tests.rs:779:9 | 779 | let foo = Foo::builder().with_x_value(1).with_y_value(2).build(); | ^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names
assert_eq!(foo, Foo { x: 1, y: 2 })

Check warning on line 780 in tests/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

consider adding a `;` to the last statement for consistent formatting

warning: consider adding a `;` to the last statement for consistent formatting --> tests/tests.rs:780:5 | 780 | assert_eq!(foo, Foo { x: 1, y: 2 }) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `assert_eq!(foo, Foo { x: 1, y: 2 });` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
}

#[test]
fn test_issue_118() {
#[derive(TypedBuilder)]
#[builder(build_method(into=Bar))]
struct Foo<T> {
#[builder(default, setter(skip))]
#[allow(dead_code)]
foo: Option<T>,
}

struct Bar;

impl<T> From<Foo<T>> for Bar {
fn from(_value: Foo<T>) -> Self {
Self
}
}

let _ = Foo::<u32>::builder().build();
}
8 changes: 7 additions & 1 deletion typed-builder-macro/src/struct_info.rs
Expand Up @@ -483,6 +483,12 @@ impl<'a> StructInfo<'a> {
} else {
quote!()
};

let type_constructor = {
let ty_generics = ty_generics.as_turbofish();
quote!(#name #ty_generics)
};

let (build_method_generic, output_type, build_method_where_clause) = match &self.builder_attr.build_method.into {
IntoSetting::NoConversion => (None, quote!(#name #ty_generics), None),
IntoSetting::GenericConversion => (
Expand All @@ -504,7 +510,7 @@ impl<'a> StructInfo<'a> {
#( #assignments )*

#[allow(deprecated)]
#name {
#type_constructor {
#( #field_names ),*
}.into()
}
Expand Down

0 comments on commit 57b6256

Please sign in to comment.