From 98df7c8474595a53c103f52f94753c25a89cf77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Sat, 23 Sep 2023 20:21:22 +0300 Subject: [PATCH] remove duplicated Eq and Hash impls --- src/lib.rs | 81 +----------------------------------------------------- 1 file changed, 1 insertion(+), 80 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ec8065e..16f10a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ type ParamIdent = syn::Ident; /// AST node type of the trait identifier such as 'Deref' in `impl> Clone for T`. /// Equality of this type doesn't compare associated bounds. Therefore `Deref` == `Deref`. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] struct TraitBound<'ast>(pub &'ast syn::Path); /// Unique name based identifier of the associated type bound such as: @@ -46,85 +46,6 @@ type AssocBoundIdent<'ast> = (&'ast ParamIdent, TraitBound<'ast>, &'ast syn::Ide // TODO: how to support GATs? make a test type AssocBoundPayload = syn::Type; -impl PartialEq for TraitBound<'_> { - fn eq(&self, other: &Self) -> bool { - if self.0.leading_colon != other.0.leading_colon { - return false; - } - - let mut first_iter = self.0.segments.iter().rev(); - let mut second_iter = other.0.segments.iter().rev(); - - let first_elem = first_iter.next().unwrap(); - let second_elem = second_iter.next().unwrap(); - - if first_elem.ident != second_elem.ident || !first_iter.eq(second_iter) { - return false; - } - - match (&first_elem.arguments, &second_elem.arguments) { - ( - syn::PathArguments::AngleBracketed(first_args), - syn::PathArguments::AngleBracketed(second_args), - ) => { - if first_args.colon2_token != second_args.colon2_token { - return false; - } - - let first_args = first_args - .args - .iter() - .filter(|arg| !matches!(arg, syn::GenericArgument::AssocType(_))) - .collect::>(); - let second_args = second_args - .args - .iter() - .filter(|arg| !matches!(arg, syn::GenericArgument::AssocType(_))) - .collect::>(); - - if first_args.len() != second_args.len() { - return false; - } - - first_args - .iter() - .zip(&second_args) - .all(|zipped_args| match zipped_args { - (syn::GenericArgument::AssocType(_), _) - | (_, syn::GenericArgument::AssocType(_)) => unreachable!(), - _ => zipped_args.0 == zipped_args.1, - }) - } - _ => first_elem.arguments == second_elem.arguments, - } - } -} - -impl Eq for TraitBound<'_> {} -impl core::hash::Hash for TraitBound<'_> { - fn hash(&self, state: &mut H) { - self.0.leading_colon.hash(state); - - let mut iter = self.0.segments.iter().rev(); - let first_elem = iter.next().unwrap(); - - iter.rev().for_each(|elem| elem.hash(state)); - first_elem.ident.hash(state); - - match &first_elem.arguments { - syn::PathArguments::AngleBracketed(first_args) => { - first_args.colon2_token.hash(state); - - first_args.args.iter().for_each(|args| match args { - syn::GenericArgument::AssocType(_) => {} - _ => args.hash(state), - }) - } - _ => first_elem.arguments.hash(state), - } - } -} - impl quote::ToTokens for TraitBound<'_> { fn to_tokens(&self, tokens: &mut TokenStream2) { self.0.leading_colon.to_tokens(tokens);