From ba7af1342e9b4ae1ed691035ba90ef3776a9126a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 30 Sep 2023 17:09:24 +0200 Subject: refactor: remove unnecessary cloning of impl generics & self type --- macros/src/injectable/implementation.rs | 32 ++++++++++++-------------------- macros/src/lib.rs | 2 +- 2 files changed, 13 insertions(+), 21 deletions(-) (limited to 'macros/src') diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs index 34c5437..a590ee4 100644 --- a/macros/src/injectable/implementation.rs +++ b/macros/src/injectable/implementation.rs @@ -3,16 +3,7 @@ use std::error::Error; use proc_macro2::{Ident, Span}; use quote::{format_ident, quote, ToTokens}; use syn::spanned::Spanned; -use syn::{ - parse_str, - ExprMethodCall, - FnArg, - Generics, - ImplItemMethod, - ItemImpl, - ReturnType, - Type, -}; +use syn::{parse_str, ExprMethodCall, FnArg, ImplItemMethod, ItemImpl, ReturnType, Type}; use crate::injectable::dependency::{DependencyError, IDependency}; use crate::util::error::diagnostic_error_enum; @@ -25,10 +16,8 @@ const DEPENDENCY_HISTORY_VAR_NAME: &str = "dependency_history"; pub struct InjectableImpl { - pub dependencies: Vec, - pub self_type: Type, - pub generics: Generics, - pub original_impl: ItemImpl, + dependencies: Vec, + original_impl: ItemImpl, constructor_method: ImplItemMethod, } @@ -71,8 +60,6 @@ impl InjectableImpl Ok(Self { dependencies, - self_type: item_impl.self_ty.as_ref().clone(), - generics: item_impl.generics.clone(), original_impl: item_impl, constructor_method, }) @@ -136,6 +123,11 @@ impl InjectableImpl Ok(()) } + pub fn self_type(&self) -> &Type + { + &self.original_impl.self_ty + } + #[cfg(not(tarpaulin_include))] pub fn expand(&self, no_doc_hidden: bool, is_async: bool) -> proc_macro2::TokenStream @@ -210,8 +202,8 @@ impl InjectableImpl get_dep_method_calls: &Vec, ) -> proc_macro2::TokenStream { - let generics = &self.generics; - let self_type = &self.self_type; + let generics = &self.original_impl.generics; + let self_type = &self.original_impl.self_ty; let constructor = &self.constructor_method.sig.ident; let dependency_idents = (0..get_dep_method_calls.len()) @@ -273,8 +265,8 @@ impl InjectableImpl get_dep_method_calls: &Vec, ) -> proc_macro2::TokenStream { - let generics = &self.generics; - let self_type = &self.self_type; + let generics = &self.original_impl.generics; + let self_type = &self.original_impl.self_ty; let constructor = &self.constructor_method.sig.ident; quote! { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 78895be..6bf5ac1 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -239,7 +239,7 @@ pub fn injectable(args_stream: TokenStream, input_stream: TokenStream) -> TokenS let expanded_injectable_impl = injectable_impl.expand(no_doc_hidden, is_async); - let self_type = &injectable_impl.self_type; + let self_type = injectable_impl.self_type(); let opt_interface = args.interface.map(Type::Path).or_else(|| { if no_declare_concrete_interface { -- cgit v1.2.3-18-g5258