git.delta.rocks / unique-network / refs/commits / 00d3414d19d7

difftreelog

feat add implementations of AbiRead & AbiWrite with macto AbiCoder

Trubnikov Sergey2022-11-14parent: #3d5704a.patch.diff
in: master

3 files changed

modifiedcrates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth
3pub(crate) fn impl_abi_macro(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {3pub(crate) fn impl_abi_macro(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
4 // dbg!(ast);4 // dbg!(ast);
5 let name = &ast.ident;5 let name = &ast.ident;
6 let (is_named_fields, field_names, field_types, params_count) = match &ast.data {
7 syn::Data::Struct(ds) => match ds.fields {
8 syn::Fields::Named(ref fields) => Ok((
9 true,
10 fields.named.iter().enumerate().map(map_field_to_name),
11 fields.named.iter().map(map_field_to_type),
12 fields.named.len(),
13 )),
14 syn::Fields::Unnamed(ref fields) => Ok((
15 false,
16 fields.unnamed.iter().enumerate().map(map_field_to_name),
17 fields.unnamed.iter().map(map_field_to_type),
18 fields.unnamed.len(),
19 )),
20 syn::Fields::Unit => Err(syn::Error::new(name.span(), "Unit structs not supported")),
21 },
22 syn::Data::Enum(_) => Err(syn::Error::new(name.span(), "Enums not supported")),
23 syn::Data::Union(_) => Err(syn::Error::new(name.span(), "Unions not supported")),
24 }?;
25
26 if params_count == 0 {
27 return Err(syn::Error::new(name.span(), "Empty structs not supported"));
28 };
29
6 let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);30 let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);
7 let abi_type = impl_abi_type(ast)?;31 let abi_type = impl_abi_type(name, field_types.clone());
8 // println!("{}", abi_type);32 let abi_read = impl_abi_read(name, is_named_fields, field_names.clone(), field_types);
33 let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names);
34 println!("{}", abi_write);
9 Ok(quote! {35 Ok(quote! {
10 #can_be_plcaed_in_vec36 #can_be_plcaed_in_vec
11 #abi_type37 #abi_type
38 #abi_read
39 #abi_write
12 })40 })
13}41}
1442
18 }46 }
19}47}
48
49fn map_field_to_name(field: (usize, &syn::Field)) -> syn::Ident {
50 match field.1.ident.as_ref() {
51 Some(name) => name.clone(),
52 None => {
53 let mut name = "field".to_string();
54 name.push_str(field.0.to_string().as_str());
55 syn::Ident::new(name.as_str(), proc_macro2::Span::call_site())
56 }
57 }
58}
2059
21fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {60fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {
22 &field.ty61 &field.ty
23}62}
2463
25fn impl_abi_type(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {64fn impl_abi_type<'a>(
26 let name = &ast.ident;65 name: &syn::Ident,
27 let (fields, params_count) = match &ast.data {
28 syn::Data::Struct(ds) => match ds.fields {
29 syn::Fields::Named(ref fields) => Ok((
30 fields.named.iter().map(map_field_to_type),
31 fields.named.len(),
32 )),
33 syn::Fields::Unnamed(ref fields) => Ok((
34 fields.unnamed.iter().map(map_field_to_type),
35 fields.unnamed.len(),
36 )),
37 syn::Fields::Unit => Err(syn::Error::new(name.span(), "Unit structs not supported")),66 field_types: impl Iterator<Item = &'a syn::Type> + Clone,
38 },67) -> proc_macro2::TokenStream {
39 syn::Data::Enum(_) => Err(syn::Error::new(name.span(), "Enums not supported")),
40 syn::Data::Union(_) => Err(syn::Error::new(name.span(), "Unions not supported")),
41 }?;
42
43 let mut params_signature = {68 let mut params_signature = {
44 let fields = fields.clone();69 let types = field_types.clone();
45 quote!(70 quote!(
46 #(nameof(<#fields as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*71 #(nameof(<#types as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*
47 )72 )
48 };73 };
49
50 if params_count == 0 {
51 return Err(syn::Error::new(name.span(), "Empty structs not supported"));
52 };
5374
54 params_signature.extend(quote!(shift_left(1)));75 params_signature.extend(quote!(shift_left(1)));
5576
56 let fields_for_dynamic = fields.clone();77 let fields_for_dynamic = field_types.clone();
5778
58 Ok(quote! {79 quote! {
59 impl ::evm_coder::abi::AbiType for #name {80 impl ::evm_coder::abi::AbiType for #name {
60 const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(81 const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(
61 new fixed("(")82 new fixed("(")
69 )*90 )*
70 }91 }
71 fn size() -> usize {92 fn size() -> usize {
72 0 #(+ <#fields as ::evm_coder::abi::AbiType>::size())*93 0 #(+ <#field_types as ::evm_coder::abi::AbiType>::size())*
73 }94 }
74 }95 }
75 })96 }
76}97}
98
99fn impl_abi_read<'a>(
100 name: &syn::Ident,
101 is_named_fields: bool,
102 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,
103 field_types: impl Iterator<Item = &'a syn::Type> + Clone,
104) -> proc_macro2::TokenStream {
105 let field_names1 = field_names.clone();
106
107 let struct_constructor = if is_named_fields {
108 quote!(Ok(Self { #(#field_names1),* }))
109 } else {
110 quote!(Ok(Self ( #(#field_names1),* )))
111 };
112 quote!(
113 impl ::evm_coder::abi::AbiRead for #name {
114 fn abi_read(reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Self> {
115 let size = if !<Self as ::evm_coder::abi::AbiType>::is_dynamic() {
116 Some(<Self as ::evm_coder::abi::AbiType>::size())
117 } else {
118 None
119 };
120 let mut subresult = reader.subresult(size)?;
121 #(
122 let #field_names = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;
123 )*
124
125 #struct_constructor
126 }
127 }
128 )
129}
130
131fn impl_abi_write<'a>(
132 name: &syn::Ident,
133 is_named_fields: bool,
134 params_count: usize,
135 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,
136) -> proc_macro2::TokenStream {
137 let abi_write = if is_named_fields {
138 quote!(
139 #(
140 self.#field_names.abi_write(writer);
141 )*
142 )
143 } else {
144 let field_names = (0..params_count)
145 .into_iter()
146 .map(proc_macro2::Literal::usize_unsuffixed);
147 quote!(
148 #(
149 self.#field_names.abi_write(writer);
150 )*
151 )
152 };
153 quote!(
154 impl ::evm_coder::abi::AbiWrite for #name {
155 fn abi_write(&self, writer: &mut ::evm_coder::abi::AbiWriter) {
156 #abi_write
157 }
158 }
159 )
160}
77161
modifiedcrates/evm-coder/src/abi/mod.rsdiffbeforeafterboth
186186
187 /// Slice recursive buffer, advance one word for buffer offset187 /// Slice recursive buffer, advance one word for buffer offset
188 /// If `size` is [`None`] then [`Self::offset`] and [`Self::subresult_offset`] evals from [`Self::buf`].188 /// If `size` is [`None`] then [`Self::offset`] and [`Self::subresult_offset`] evals from [`Self::buf`].
189 fn subresult(&mut self, size: Option<usize>) -> Result<AbiReader<'i>> {189 pub fn subresult(&mut self, size: Option<usize>) -> Result<AbiReader<'i>> {
190 let subresult_offset = self.subresult_offset;190 let subresult_offset = self.subresult_offset;
191 let offset = if let Some(size) = size {191 let offset = if let Some(size) = size {
192 self.offset += size;192 self.offset += size;
modifiedcrates/evm-coder/tests/abi_derive_generation.rsdiffbeforeafterboth
406 );406 );
407}407}
408
409// #[test]
410// fn impl_abi_read() {
411// TypeStruct1SimpleParam::
412// }
408413