Skip to content

Commit

Permalink
Allow Cow inner type to derive Into
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Jun 1, 2024
1 parent 5b6548c commit ba09d52
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 8 deletions.
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 @@ -155,7 +155,7 @@ fn gen_implemented_traits(
.map(|t| match t {
AnyIrregularTrait::AsRef => Ok(gen_impl_trait_as_ref(type_name, inner_type)),
AnyIrregularTrait::From => Ok(gen_impl_trait_from(type_name, inner_type)),
AnyIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, inner_type.clone())),
AnyIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, generics, inner_type.clone())),
AnyIrregularTrait::Display => Ok(gen_impl_trait_display(type_name, generics)),
AnyIrregularTrait::Deref => Ok(gen_impl_trait_deref(type_name, inner_type)),
AnyIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, inner_type)),
Expand Down
10 changes: 7 additions & 3 deletions nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ where
}
}

pub fn gen_impl_trait_into(type_name: &TypeName, inner_type: impl Into<InnerType>) -> TokenStream {
pub fn gen_impl_trait_into(
type_name: &TypeName,
generics: &Generics,
inner_type: impl Into<InnerType>,
) -> TokenStream {
let inner_type: InnerType = inner_type.into();

// NOTE: We're getting blank implementation of
// Into<Inner> for Type
// by implementing
// From<Type> for Inner
quote! {
impl ::core::convert::From<#type_name> for #inner_type {
impl #generics ::core::convert::From<#type_name #generics> for #inner_type {
#[inline]
fn from(value: #type_name) -> Self {
fn from(value: #type_name #generics) -> Self {
value.into_inner()
}
}
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 @@ -172,7 +172,7 @@ fn gen_implemented_traits<T: ToTokens>(
Ok(gen_impl_trait_from_str(type_name, inner_type, maybe_error_type_name.as_ref()))
}
FloatIrregularTrait::From => Ok(gen_impl_trait_from(type_name, inner_type)),
FloatIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, inner_type)),
FloatIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, &Generics::default(), inner_type)),
FloatIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
}
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 @@ -192,7 +192,7 @@ fn gen_implemented_traits<T: ToTokens>(
Ok(gen_impl_trait_from_str(type_name, inner_type, maybe_error_type_name.as_ref()))
}
IntegerIrregularTrait::From => Ok(gen_impl_trait_from(type_name, inner_type)),
IntegerIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, inner_type)),
IntegerIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, &Generics::default(), inner_type)),
IntegerIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
}
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/string/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn gen_implemented_traits(
Ok(gen_impl_from_str(type_name, maybe_error_type_name.as_ref()))
}
StringIrregularTrait::From => Ok(gen_impl_from_str_and_string(type_name)),
StringIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, inner_type)),
StringIrregularTrait::Into => Ok(gen_impl_trait_into(type_name, &Generics::default(), inner_type)),
StringIrregularTrait::TryFrom => {
Ok(gen_impl_try_from(type_name, maybe_error_type_name.as_ref()))
}
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ mod with_generics {
fn test_generic_with_lifetime_cow() {
#[nutype(
validate(predicate = |s| s.len() >= 3),
derive(Debug, Display)
derive(Debug, Display, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Into)
)]
struct Clarabelle<'a>(Cow<'a, str>);

Expand Down

0 comments on commit ba09d52

Please sign in to comment.