Skip to content

Commit

Permalink
Make it possible to derive Borrow 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 6586ede commit fbf73dc
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 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 @@ -24,7 +24,7 @@ struct NotEmpty<T>(Vec<T>);
Into,
From,
Deref,
// Borrow,
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 @@ -158,7 +158,7 @@ fn gen_implemented_traits(
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, generics, inner_type)),
AnyIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, inner_type)),
AnyIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, generics, inner_type)),
AnyIrregularTrait::FromStr => Ok(
gen_impl_trait_from_str(type_name, inner_type, maybe_error_type_name.as_ref())
),
Expand Down
8 changes: 6 additions & 2 deletions nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ pub fn gen_impl_trait_display(type_name: &TypeName, generics: &Generics) -> Toke
}
}

pub fn gen_impl_trait_borrow(type_name: &TypeName, borrowed_type: impl ToTokens) -> TokenStream {
pub fn gen_impl_trait_borrow(
type_name: &TypeName,
generics: &Generics,
borrowed_type: impl ToTokens,
) -> TokenStream {
quote! {
impl ::core::borrow::Borrow<#borrowed_type> for #type_name {
impl #generics ::core::borrow::Borrow<#borrowed_type> for #type_name #generics {
#[inline]
fn borrow(&self) -> &#borrowed_type {
&self.0
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 @@ -179,7 +179,7 @@ fn gen_implemented_traits<T: ToTokens>(
FloatIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
}
FloatIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, inner_type)),
FloatIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, generics, inner_type)),
FloatIrregularTrait::Display => Ok(gen_impl_trait_display(type_name, generics)),
FloatIrregularTrait::Default => match maybe_default_value {
Some(ref default_value) => {
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 @@ -199,7 +199,7 @@ fn gen_implemented_traits<T: ToTokens>(
IntegerIrregularTrait::TryFrom => {
Ok(gen_impl_trait_try_from(type_name, inner_type, maybe_error_type_name.as_ref()))
}
IntegerIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, inner_type)),
IntegerIrregularTrait::Borrow => Ok(gen_impl_trait_borrow(type_name, generics, inner_type)),
IntegerIrregularTrait::Display => Ok(gen_impl_trait_display(type_name, generics)),
IntegerIrregularTrait::Default => {
match maybe_default_value {
Expand Down
5 changes: 3 additions & 2 deletions nutype_macros/src/string/gen/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ fn gen_impl_try_from(
}

fn gen_impl_borrow_str_and_string(type_name: &TypeName) -> TokenStream {
let impl_borrow_string = gen_impl_trait_borrow(type_name, quote!(String));
let impl_borrow_str = gen_impl_trait_borrow(type_name, quote!(str));
let generics = Generics::default();
let impl_borrow_string = gen_impl_trait_borrow(type_name, &generics, quote!(String));
let impl_borrow_str = gen_impl_trait_borrow(type_name, &generics, quote!(str));

quote! {
#impl_borrow_string
Expand Down

0 comments on commit fbf73dc

Please sign in to comment.