Skip to content

Commit

Permalink
Make it possible to derive TryFrom for an inner type with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Jun 1, 2024
1 parent fbf73dc commit b0ed3e2
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/any_generics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct NotEmpty<T>(Vec<T>);
// TODO
// AsRef,
// FromStr,
Into,
From,
Deref,
Borrow,
// FromStr,
// TryFrom,
// Default,
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/any/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn gen_implemented_traits(
gen_impl_trait_from_str(type_name, inner_type, maybe_error_type_name.as_ref())
),
AnyIrregularTrait::TryFrom => Ok(
gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref())
gen_impl_trait_try_from(type_name, generics, inner_type, maybe_error_type_name.as_ref())
),
AnyIrregularTrait::Default => match maybe_default_value {
Some(ref default_value) => {
Expand Down
9 changes: 5 additions & 4 deletions nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub fn gen_impl_trait_from(

pub fn gen_impl_trait_try_from(
type_name: &TypeName,
generics: &Generics,
inner_type: impl ToTokens,
maybe_error_type_name: Option<&ErrorTypeName>,
) -> TokenStream {
Expand All @@ -160,11 +161,11 @@ pub fn gen_impl_trait_try_from(
// The case when there are validation
//
quote! {
impl ::core::convert::TryFrom<#inner_type> for #type_name {
impl #generics ::core::convert::TryFrom<#inner_type> for #type_name #generics {
type Error = #error_type_name;

#[inline]
fn try_from(raw_value: #inner_type) -> Result<#type_name, Self::Error> {
fn try_from(raw_value: #inner_type) -> Result<#type_name #generics, Self::Error> {
Self::new(raw_value)
}
}
Expand All @@ -174,11 +175,11 @@ pub fn gen_impl_trait_try_from(
// The case when there are no validation
//
quote! {
impl ::core::convert::TryFrom<#inner_type> for #type_name {
impl #generics ::core::convert::TryFrom<#inner_type> for #type_name #generics {
type Error = ::core::convert::Infallible;

#[inline]
fn try_from(raw_value: #inner_type) -> Result<#type_name, Self::Error> {
fn try_from(raw_value: #inner_type) -> Result<#type_name #generics, Self::Error> {
Ok(Self::new(raw_value))
}
}
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/float/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn gen_implemented_traits<T: ToTokens>(
FloatIrregularTrait::From => Ok(gen_impl_trait_from(type_name, generics, inner_type)),
FloatIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, generics, inner_type)),
FloatIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
Ok(gen_impl_trait_try_from(type_name, generics, inner_type, maybe_error_type_name.as_ref()))
}
FloatIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, generics, inner_type)),
FloatIrregularTrait::Display => Ok(gen_impl_trait_display(type_name, generics)),
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/integer/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn gen_implemented_traits<T: ToTokens>(
IntegerIrregularTrait::From => Ok(gen_impl_trait_from(type_name, generics, inner_type)),
IntegerIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, generics, inner_type)),
IntegerIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
Ok(gen_impl_trait_try_from(type_name, generics, inner_type, maybe_error_type_name.as_ref()))
}
IntegerIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, generics, inner_type)),
IntegerIrregularTrait::Display => Ok(gen_impl_trait_display(type_name, generics)),
Expand Down
6 changes: 4 additions & 2 deletions nutype_macros/src/string/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ fn gen_impl_try_from(
type_name: &TypeName,
maybe_error_type_name: Option<&ErrorTypeName>,
) -> TokenStream {
let generics = Generics::default();
let impl_try_from_string =
gen_impl_trait_try_from(type_name, quote!(String), maybe_error_type_name);
let impl_try_from_str = gen_impl_trait_try_from(type_name, quote!(&str), maybe_error_type_name);
gen_impl_trait_try_from(type_name, &generics, quote!(String), maybe_error_type_name);
let impl_try_from_str =
gen_impl_trait_try_from(type_name, &generics, quote!(&str), maybe_error_type_name);

quote! {
#impl_try_from_string
Expand Down
3 changes: 1 addition & 2 deletions test_suite/tests/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,7 @@ mod with_generics {
fn test_generic_with_lifetime_cow() {
#[nutype(
validate(predicate = |s| s.len() >= 3),
// TODO: derive TryFrom
derive(Debug, Display, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Into, Deref)
derive(Debug, Display, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Into, Deref, Borrow, TryFrom)
)]
struct Clarabelle<'a>(Cow<'a, str>);

Expand Down

0 comments on commit b0ed3e2

Please sign in to comment.