From c75d97772ca4b5141ad25c73d924c9f4508a9b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 11 Nov 2022 17:53:18 +0100 Subject: [PATCH] identifiers: Deduplicate as_str() based impls between ID DSTs and owned IDs --- crates/ruma-macros/src/identifiers.rs | 79 +++++++++++---------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index eace9d3e..a03b699b 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -70,7 +70,7 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { let id_ty = quote! { #id #ty_generics }; let owned_ty = quote! { #owned #ty_generics }; - let partial_eq_string = expand_partial_eq_string(id_ty.clone(), &impl_generics); + let as_str_impls = expand_as_str_impls(id_ty.clone(), &impl_generics); // FIXME: Remove? let box_partial_eq_string = expand_partial_eq_string(quote! { Box<#id_ty> }, &impl_generics); @@ -193,28 +193,7 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { } } - impl #impl_generics std::fmt::Debug for #id_ty { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(self.as_str(), f) - } - } - - impl #impl_generics std::fmt::Display for #id_ty { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_str()) - } - } - - impl #impl_generics serde::Serialize for #id_ty { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(self.as_str()) - } - } - - #partial_eq_string + #as_str_impls #box_partial_eq_string #extra_impls }) @@ -230,7 +209,7 @@ fn expand_owned_id(input: &ItemStruct) -> TokenStream { let id_ty = quote! { #id #ty_generics }; let owned_ty = quote! { #owned #ty_generics }; - let partial_eq_string = expand_partial_eq_string(owned_ty.clone(), &impl_generics); + let as_str_impls = expand_as_str_impls(owned_ty.clone(), &impl_generics); quote! { #[doc = #doc_header] @@ -331,18 +310,6 @@ fn expand_owned_id(input: &ItemStruct) -> TokenStream { } } - impl #impl_generics std::fmt::Display for #owned_ty { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_str()) - } - } - - impl #impl_generics std::fmt::Debug for #owned_ty { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(self.as_str(), f) - } - } - impl #impl_generics std::cmp::PartialEq for #owned_ty { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() @@ -372,16 +339,7 @@ fn expand_owned_id(input: &ItemStruct) -> TokenStream { } } - impl #impl_generics serde::Serialize for #owned_ty { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(self.as_str()) - } - } - - #partial_eq_string + #as_str_impls impl #impl_generics PartialEq<#id_ty> for #owned_ty { fn eq(&self, other: &#id_ty) -> bool { @@ -655,6 +613,35 @@ fn expand_unchecked_impls(input: &ItemStruct) -> TokenStream { } } +fn expand_as_str_impls(ty: TokenStream, impl_generics: &ImplGenerics<'_>) -> TokenStream { + let partial_eq_string = expand_partial_eq_string(ty.clone(), impl_generics); + + quote! { + impl #impl_generics std::fmt::Display for #ty { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } + } + + impl #impl_generics std::fmt::Debug for #ty { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + ::fmt(self.as_str(), f) + } + } + + impl #impl_generics serde::Serialize for #ty { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.as_str()) + } + } + + #partial_eq_string + } +} + fn expand_partial_eq_string(ty: TokenStream, impl_generics: &ImplGenerics<'_>) -> TokenStream { IntoIterator::into_iter([ (ty.clone(), quote! { str }),