git.delta.rocks / unique-network / refs/commits / 9b94d7cf0b98

difftreelog

feat allow generics in new coder macros

Yaroslav Bolyukin2021-07-20parent: #5262156.patch.diff
in: master

3 files changed

modifiedcrates/evm-coder-macros/src/lib.rsdiffbeforeafterboth
103 }103 }
104}104}
105105
106fn parse_ident_from_segment(segment: &PathSegment) -> syn::Result<&Ident> {106fn parse_ident_from_segment(segment: &PathSegment, allow_generics: bool) -> syn::Result<&Ident> {
107 if segment.arguments != PathArguments::None {107 if segment.arguments != PathArguments::None && !allow_generics {
108 return Err(syn::Error::new(108 return Err(syn::Error::new(
109 segment.arguments.span(),109 segment.arguments.span(),
110 "unexpected generic type",110 "unexpected generic type",
113 Ok(&segment.ident)113 Ok(&segment.ident)
114}114}
115115
116fn parse_ident_from_path(path: &Path) -> syn::Result<&Ident> {116fn parse_ident_from_path(path: &Path, allow_generics: bool) -> syn::Result<&Ident> {
117 let segment = parse_path_segment(path)?;117 let segment = parse_path_segment(path)?;
118 parse_ident_from_segment(segment)118 parse_ident_from_segment(segment, allow_generics)
119}119}
120120
121fn parse_ident_from_type(ty: &Type) -> syn::Result<&Ident> {121fn parse_ident_from_type(ty: &Type, allow_generics: bool) -> syn::Result<&Ident> {
122 let path = parse_path(ty)?;122 let path = parse_path(ty)?;
123 parse_ident_from_path(path)123 parse_ident_from_path(path, allow_generics)
124}124}
125125
126// Gets T out of Result<T>126// Gets T out of Result<T>
modifiedcrates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/solidity_interface.rs
+++ b/crates/evm-coder-macros/src/solidity_interface.rs
@@ -4,10 +4,7 @@
 use darling::FromMeta;
 use inflector::cases;
 use std::fmt::Write;
-use syn::{
-	FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, Meta, NestedMeta, PatType, Path, ReturnType,
-	Type, spanned::Spanned,
-};
+use syn::{FnArg, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Meta, NestedMeta, PatType, Path, ReturnType, Type, spanned::Spanned};
 
 use crate::{
 	fn_selector_str, parse_ident_from_pat, parse_ident_from_path, parse_ident_from_type,
@@ -22,7 +19,7 @@
 }
 impl Is {
 	fn try_from(path: &Path) -> syn::Result<Self> {
-		let name = parse_ident_from_path(path)?.clone();
+		let name = parse_ident_from_path(path, false)?.clone();
 		Ok(Self {
 			pascal_call_name: pascal_ident_to_call(&name),
 			snake_call_name: pascal_ident_to_snake_call(&name),
@@ -115,7 +112,7 @@
 	fn try_from(value: &PatType) -> syn::Result<Self> {
 		Ok(Self {
 			name: parse_ident_from_pat(&value.pat)?.clone(),
-			ty: parse_ident_from_type(&value.ty)?.clone(),
+			ty: parse_ident_from_type(&value.ty, false)?.clone(),
 		})
 	}
 	fn is_value(&self) -> bool {
@@ -193,7 +190,7 @@
 			rename_selector: None,
 		};
 		for attr in &value.attrs {
-			let ident = parse_ident_from_path(&attr.path)?;
+			let ident = parse_ident_from_path(&attr.path, false)?;
 			if ident == "solidity" {
 				let args = attr.parse_meta().unwrap();
 				info = MethodInfo::from_meta(&args).unwrap();
@@ -382,8 +379,8 @@
 }
 
 pub struct SolidityInterface {
+    generics: Generics,
 	name: Box<syn::Type>,
-	ident: Ident,
 	info: InterfaceInfo,
 	methods: Vec<Method>,
 }
@@ -397,8 +394,8 @@
 			}
 		}
 		Ok(Self {
+            generics: value.generics.clone(),
 			name: value.self_ty.clone(),
-			ident: parse_ident_from_type(&value.self_ty)?.clone(),
 			info,
 			methods,
 		})
@@ -407,6 +404,7 @@
 		let name = self.name;
 
 		let call_name = pascal_ident_to_call(&self.info.name);
+        let generics = self.generics;
 
 		let call_sub = self
 			.info
@@ -485,7 +483,7 @@
 					return Ok(None);
 				}
 			}
-			impl ::evm_coder::Callable<#call_name> for #name {
+			impl #generics ::evm_coder::Callable<#call_name> for #name {
 				#[allow(unreachable_code)] // In case of no inner calls
 				fn call(&mut self, c: Msg<#call_name>) -> Result<::evm_coder::abi::AbiWriter> {
 					use ::evm_coder::abi::AbiWrite;
modifiedcrates/evm-coder-macros/src/to_log.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/to_log.rs
+++ b/crates/evm-coder-macros/src/to_log.rs
@@ -13,10 +13,10 @@
 impl EventField {
 	fn try_from(field: &Field) -> syn::Result<Self> {
 		let name = field.ident.as_ref().unwrap();
-		let ty = parse_ident_from_type(&field.ty)?;
+		let ty = parse_ident_from_type(&field.ty, false)?;
 		let mut indexed = false;
 		for attr in &field.attrs {
-			if let Ok(ident) = parse_ident_from_path(&attr.path) {
+			if let Ok(ident) = parse_ident_from_path(&attr.path, false) {
 				if ident == "indexed" {
 					indexed = true;
 				}