From 57b6256b780cd8813addeb169ccccefcbdfea8c1 Mon Sep 17 00:00:00 2001 From: IdanArye Date: Fri, 22 Sep 2023 01:33:32 +0300 Subject: [PATCH] Fix #118: Use generics with the constructor in `build` method --- CHANGELOG.md | 3 +++ tests/tests.rs | 21 +++++++++++++++++++++ typed-builder-macro/src/struct_info.rs | 8 +++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9220d555..2eee9600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tests/tests.rs b/tests/tests.rs index 0a84be96..44cba043 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -779,3 +779,24 @@ fn test_prefix_and_suffix() { let foo = Foo::builder().with_x_value(1).with_y_value(2).build(); assert_eq!(foo, Foo { x: 1, y: 2 }) } + +#[test] +fn test_issue_118() { + #[derive(TypedBuilder)] + #[builder(build_method(into=Bar))] + struct Foo { + #[builder(default, setter(skip))] + #[allow(dead_code)] + foo: Option, + } + + struct Bar; + + impl From> for Bar { + fn from(_value: Foo) -> Self { + Self + } + } + + let _ = Foo::::builder().build(); +} diff --git a/typed-builder-macro/src/struct_info.rs b/typed-builder-macro/src/struct_info.rs index b245d22c..c77c9754 100644 --- a/typed-builder-macro/src/struct_info.rs +++ b/typed-builder-macro/src/struct_info.rs @@ -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 => ( @@ -504,7 +510,7 @@ impl<'a> StructInfo<'a> { #( #assignments )* #[allow(deprecated)] - #name { + #type_constructor { #( #field_names ),* }.into() }