difftreelog
feat add FIELDS_COUNT const in AbiType trait
in: master
6 files changed
crates/evm-coder/procedural/src/abi_derive/derive_enum.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/abi_derive/derive_enum.rs
+++ b/crates/evm-coder/procedural/src/abi_derive/derive_enum.rs
@@ -47,10 +47,11 @@
)
}
-pub fn impl_enum_abi_type(name: &syn::Ident) -> proc_macro2::TokenStream {
+pub fn impl_enum_abi_type(name: &syn::Ident, option_count: usize) -> proc_macro2::TokenStream {
quote! {
impl ::evm_coder::abi::AbiType for #name {
const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = <u8 as ::evm_coder::abi::AbiType>::SIGNATURE;
+ const FIELDS_COUNT: usize = #option_count;
fn is_dynamic() -> bool {
<u8 as ::evm_coder::abi::AbiType>::is_dynamic()
crates/evm-coder/procedural/src/abi_derive/derive_struct.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/abi_derive/derive_struct.rs
+++ b/crates/evm-coder/procedural/src/abi_derive/derive_struct.rs
@@ -100,10 +100,12 @@
pub fn impl_struct_abi_type(
name: &syn::Ident,
tuple_type: proc_macro2::TokenStream,
+ fields_count: usize,
) -> proc_macro2::TokenStream {
quote! {
impl ::evm_coder::abi::AbiType for #name {
const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = <#tuple_type as ::evm_coder::abi::AbiType>::SIGNATURE;
+ const FIELDS_COUNT: usize = #fields_count;
fn is_dynamic() -> bool {
<#tuple_type as ::evm_coder::abi::AbiType>::is_dynamic()
}
crates/evm-coder/procedural/src/abi_derive/mod.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/abi_derive/mod.rs
+++ b/crates/evm-coder/procedural/src/abi_derive/mod.rs
@@ -49,7 +49,7 @@
let struct_from_tuple = struct_from_tuple(name, is_named_fields, field_names.clone());
let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);
- let abi_type = impl_struct_abi_type(name, tuple_type.clone());
+ let abi_type = impl_struct_abi_type(name, tuple_type.clone(), params_count);
let abi_read = impl_struct_abi_read(name, tuple_type, tuple_names, struct_from_tuple);
let abi_write = impl_struct_abi_write(name, is_named_fields, tuple_ref_type, tuple_data);
let solidity_type = impl_struct_solidity_type(name, field_types.clone(), params_count);
@@ -83,7 +83,7 @@
let from = impl_enum_from_u8(name, enum_options.clone());
let solidity_option = impl_solidity_option(name, enum_options.clone());
let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);
- let abi_type = impl_enum_abi_type(name);
+ let abi_type = impl_enum_abi_type(name, option_count);
let abi_read = impl_enum_abi_read(name);
let abi_write = impl_enum_abi_write(name);
let solidity_type_name = impl_enum_solidity_type_name(name);
crates/evm-coder/src/abi/impls.rsdiffbeforeafterboth1use crate::{2 custom_signature::SignatureUnit,3 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},4 make_signature, sealed,5 types::*,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));1920 fn is_dynamic() -> bool {21 $dynamic22 }2324 fn size() -> usize {25 ABI_ALIGNMENT26 }27 }28 };29}3031macro_rules! impl_abi_readable {32 ($ty:ty, $method:ident) => {33 impl AbiRead for $ty {34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {35 reader.$method()36 }37 }38 };39}4041macro_rules! impl_abi_writeable {42 ($ty:ty, $method:ident) => {43 impl AbiWrite for $ty {44 fn abi_write(&self, writer: &mut AbiWriter) {45 writer.$method(&self)46 }47 }48 };49}5051macro_rules! impl_abi {52 ($ty:ty, $method:ident, $dynamic:literal) => {53 impl_abi_type!($ty, $method, $dynamic);54 impl_abi_readable!($ty, $method);55 impl_abi_writeable!($ty, $method);56 };57}5859impl_abi!(bool, bool, false);60impl_abi!(u8, uint8, false);61impl_abi!(u32, uint32, false);62impl_abi!(u64, uint64, false);63impl_abi!(u128, uint128, false);64impl_abi!(U256, uint256, false);65impl_abi!(H160, address, false);66impl_abi!(string, string, true);6768impl_abi_writeable!(&str, string);6970impl_abi_type!(bytes, bytes, true);7172impl AbiRead for bytes {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {74 Ok(bytes(reader.bytes()?))75 }76}7778impl AbiWrite for bytes {79 fn abi_write(&self, writer: &mut AbiWriter) {80 writer.bytes(self.0.as_slice())81 }82}8384impl_abi_type!(bytes4, bytes4, false);85impl AbiRead for bytes4 {86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {87 reader.bytes4()88 }89}9091impl<T: AbiWrite> AbiWrite for &T {92 fn abi_write(&self, writer: &mut AbiWriter) {93 T::abi_write(self, writer);94 }95}9697impl<T: AbiType> AbiType for &T {98 const SIGNATURE: SignatureUnit = T::SIGNATURE;99100 fn is_dynamic() -> bool {101 T::is_dynamic()102 }103104 fn size() -> usize {105 T::size()106 }107}108109impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {110 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {111 let mut sub = reader.subresult(None)?;112 let size = sub.uint32()? as usize;113 sub.subresult_offset = sub.offset;114 let is_dynamic = <T as AbiType>::is_dynamic();115 let mut out = Vec::with_capacity(size);116 for _ in 0..size {117 out.push(<T as AbiRead>::abi_read(&mut sub)?);118 if !is_dynamic {119 sub.bytes_read(<T as AbiType>::size());120 };121 }122 Ok(out)123 }124}125126impl<T: AbiType> AbiType for Vec<T> {127 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));128129 fn is_dynamic() -> bool {130 true131 }132133 fn size() -> usize {134 ABI_ALIGNMENT135 }136}137138impl sealed::CanBePlacedInVec for Property {}139140impl AbiType for Property {141 const SIGNATURE: SignatureUnit = make_signature!(new fixed("(string,bytes)"));142143 fn is_dynamic() -> bool {144 string::is_dynamic() || bytes::is_dynamic()145 }146147 fn size() -> usize {148 <string as AbiType>::size() + <bytes as AbiType>::size()149 }150}151152impl AbiRead for Property {153 fn abi_read(reader: &mut AbiReader) -> Result<Property> {154 let size = if !Property::is_dynamic() {155 Some(<Property as AbiType>::size())156 } else {157 None158 };159 let mut subresult = reader.subresult(size)?;160 let key = <string>::abi_read(&mut subresult)?;161 let value = <bytes>::abi_read(&mut subresult)?;162163 Ok(Property { key, value })164 }165}166167impl AbiWrite for Property {168 fn abi_write(&self, writer: &mut AbiWriter) {169 (&self.key, &self.value).abi_write(writer);170 }171}172173impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {174 fn abi_write(&self, writer: &mut AbiWriter) {175 let is_dynamic = T::is_dynamic();176 let mut sub = if is_dynamic {177 AbiWriter::new_dynamic(is_dynamic)178 } else {179 AbiWriter::new()180 };181182 // Write items count183 (self.len() as u32).abi_write(&mut sub);184185 for item in self {186 item.abi_write(&mut sub);187 }188 writer.write_subresult(sub);189 }190}191192impl AbiWrite for () {193 fn abi_write(&self, _writer: &mut AbiWriter) {}194}195196/// This particular AbiWrite implementation should be split to another trait,197/// which only implements `to_result`, but due to lack of specialization feature198/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,199/// so here we abusing default trait methods for it200impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {201 fn abi_write(&self, _writer: &mut AbiWriter) {202 debug_assert!(false, "shouldn't be called, see comment")203 }204 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {205 match self {206 Ok(v) => Ok(WithPostDispatchInfo {207 post_info: v.post_info.clone(),208 data: {209 let mut out = AbiWriter::new();210 v.data.abi_write(&mut out);211 out212 },213 }),214 Err(e) => Err(e.clone()),215 }216 }217}218219macro_rules! impl_tuples {220 ($($ident:ident)+) => {221 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)222 where223 $(224 $ident: AbiType,225 )+226 {227 const SIGNATURE: SignatureUnit = make_signature!(228 new fixed("(")229 $(nameof(<$ident>::SIGNATURE) fixed(","))+230 shift_left(1)231 fixed(")")232 );233234 fn is_dynamic() -> bool {235 false236 $(237 || <$ident>::is_dynamic()238 )*239 }240241 fn size() -> usize {242 0 $(+ <$ident>::size())+243 }244 }245246 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}247248 impl<$($ident),+> AbiRead for ($($ident,)+)249 where250 Self: AbiType,251 $($ident: AbiRead + AbiType,)+252 {253 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {254 let is_dynamic = <Self>::is_dynamic();255 let size = if !is_dynamic { Some(<Self>::size()) } else { None };256 let mut subresult = reader.subresult(size)?;257 Ok((258 $({259 let value = <$ident>::abi_read(&mut subresult)?;260 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};261 value262 },)+263 ))264 }265 }266267 #[allow(non_snake_case)]268 impl<$($ident),+> AbiWrite for ($($ident,)+)269 where270 $($ident: AbiWrite + AbiType,)+271 {272 fn abi_write(&self, writer: &mut AbiWriter) {273 let ($($ident,)+) = self;274 if <Self as AbiType>::is_dynamic() {275 let mut sub = AbiWriter::new();276 $($ident.abi_write(&mut sub);)+277 writer.write_subresult(sub);278 } else {279 $($ident.abi_write(writer);)+280 }281 }282 }283 };284}285286impl_tuples! {A}287impl_tuples! {A B}288impl_tuples! {A B C}289impl_tuples! {A B C D}290impl_tuples! {A B C D E}291impl_tuples! {A B C D E F}292impl_tuples! {A B C D E F G}293impl_tuples! {A B C D E F G H}294impl_tuples! {A B C D E F G H I}295impl_tuples! {A B C D E F G H I J}1use crate::{2 custom_signature::SignatureUnit,3 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},4 make_signature, sealed,5 types::*,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));19 const FIELDS_COUNT: usize = 1;2021 fn is_dynamic() -> bool {22 $dynamic23 }2425 fn size() -> usize {26 ABI_ALIGNMENT27 }28 }29 };30}3132macro_rules! impl_abi_readable {33 ($ty:ty, $method:ident) => {34 impl AbiRead for $ty {35 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {36 reader.$method()37 }38 }39 };40}4142macro_rules! impl_abi_writeable {43 ($ty:ty, $method:ident) => {44 impl AbiWrite for $ty {45 fn abi_write(&self, writer: &mut AbiWriter) {46 writer.$method(&self)47 }48 }49 };50}5152macro_rules! impl_abi {53 ($ty:ty, $method:ident, $dynamic:literal) => {54 impl_abi_type!($ty, $method, $dynamic);55 impl_abi_readable!($ty, $method);56 impl_abi_writeable!($ty, $method);57 };58}5960impl_abi!(bool, bool, false);61impl_abi!(u8, uint8, false);62impl_abi!(u32, uint32, false);63impl_abi!(u64, uint64, false);64impl_abi!(u128, uint128, false);65impl_abi!(U256, uint256, false);66impl_abi!(H160, address, false);67impl_abi!(string, string, true);6869impl_abi_writeable!(&str, string);7071impl_abi_type!(bytes, bytes, true);7273impl AbiRead for bytes {74 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {75 Ok(bytes(reader.bytes()?))76 }77}7879impl AbiWrite for bytes {80 fn abi_write(&self, writer: &mut AbiWriter) {81 writer.bytes(self.0.as_slice())82 }83}8485impl_abi_type!(bytes4, bytes4, false);86impl AbiRead for bytes4 {87 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {88 reader.bytes4()89 }90}9192impl<T: AbiWrite> AbiWrite for &T {93 fn abi_write(&self, writer: &mut AbiWriter) {94 T::abi_write(self, writer);95 }96}9798impl<T: AbiType> AbiType for &T {99 const SIGNATURE: SignatureUnit = T::SIGNATURE;100 const FIELDS_COUNT: usize = T::FIELDS_COUNT;101102 fn is_dynamic() -> bool {103 T::is_dynamic()104 }105106 fn size() -> usize {107 T::size()108 }109}110111impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {112 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {113 let mut sub = reader.subresult(None)?;114 let size = sub.uint32()? as usize;115 sub.subresult_offset = sub.offset;116 let is_dynamic = <T as AbiType>::is_dynamic();117 let mut out = Vec::with_capacity(size);118 for _ in 0..size {119 out.push(<T as AbiRead>::abi_read(&mut sub)?);120 if !is_dynamic {121 sub.bytes_read(<T as AbiType>::size());122 };123 }124 Ok(out)125 }126}127128impl<T: AbiType> AbiType for Vec<T> {129 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));130 const FIELDS_COUNT: usize = 1;131132 fn is_dynamic() -> bool {133 true134 }135136 fn size() -> usize {137 ABI_ALIGNMENT138 }139}140141impl sealed::CanBePlacedInVec for Property {}142143impl AbiType for Property {144 const SIGNATURE: SignatureUnit = make_signature!(new fixed("(string,bytes)"));145 const FIELDS_COUNT: usize = 2;146147 fn is_dynamic() -> bool {148 string::is_dynamic() || bytes::is_dynamic()149 }150151 fn size() -> usize {152 <string as AbiType>::size() + <bytes as AbiType>::size()153 }154}155156impl AbiRead for Property {157 fn abi_read(reader: &mut AbiReader) -> Result<Property> {158 let size = if !Property::is_dynamic() {159 Some(<Property as AbiType>::size())160 } else {161 None162 };163 let mut subresult = reader.subresult(size)?;164 let key = <string>::abi_read(&mut subresult)?;165 let value = <bytes>::abi_read(&mut subresult)?;166167 Ok(Property { key, value })168 }169}170171impl AbiWrite for Property {172 fn abi_write(&self, writer: &mut AbiWriter) {173 (&self.key, &self.value).abi_write(writer);174 }175}176177impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {178 fn abi_write(&self, writer: &mut AbiWriter) {179 let is_dynamic = T::is_dynamic();180 let mut sub = if is_dynamic {181 AbiWriter::new_dynamic(is_dynamic)182 } else {183 AbiWriter::new()184 };185186 // Write items count187 (self.len() as u32).abi_write(&mut sub);188189 for item in self {190 item.abi_write(&mut sub);191 }192 writer.write_subresult(sub);193 }194}195196impl AbiWrite for () {197 fn abi_write(&self, _writer: &mut AbiWriter) {}198}199200/// This particular AbiWrite implementation should be split to another trait,201/// which only implements `to_result`, but due to lack of specialization feature202/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,203/// so here we abusing default trait methods for it204impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {205 fn abi_write(&self, _writer: &mut AbiWriter) {206 debug_assert!(false, "shouldn't be called, see comment")207 }208 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {209 match self {210 Ok(v) => Ok(WithPostDispatchInfo {211 post_info: v.post_info.clone(),212 data: {213 let mut out = AbiWriter::new();214 v.data.abi_write(&mut out);215 out216 },217 }),218 Err(e) => Err(e.clone()),219 }220 }221}222223macro_rules! impl_tuples {224 ($($ident:ident)+) => {225 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)226 where227 $(228 $ident: AbiType,229 )+230 {231 const SIGNATURE: SignatureUnit = make_signature!(232 new fixed("(")233 $(nameof(<$ident>::SIGNATURE) fixed(","))+234 shift_left(1)235 fixed(")")236 );237 const FIELDS_COUNT: usize = 0 $(+ {let _ = <$ident as AbiType>::FIELDS_COUNT; 1})+;238239 fn is_dynamic() -> bool {240 false241 $(242 || <$ident>::is_dynamic()243 )*244 }245246 fn size() -> usize {247 0 $(+ <$ident>::size())+248 }249 }250251 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}252253 impl<$($ident),+> AbiRead for ($($ident,)+)254 where255 Self: AbiType,256 $($ident: AbiRead + AbiType,)+257 {258 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {259 let is_dynamic = <Self>::is_dynamic();260 let size = if !is_dynamic { Some(<Self>::size()) } else { None };261 let mut subresult = reader.subresult(size)?;262 Ok((263 $({264 let value = <$ident>::abi_read(&mut subresult)?;265 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};266 value267 },)+268 ))269 }270 }271272 #[allow(non_snake_case)]273 impl<$($ident),+> AbiWrite for ($($ident,)+)274 where275 $($ident: AbiWrite + AbiType,)+276 {277 fn abi_write(&self, writer: &mut AbiWriter) {278 let ($($ident,)+) = self;279 if <Self as AbiType>::is_dynamic() {280 let mut sub = AbiWriter::new();281 $($ident.abi_write(&mut sub);)+282 writer.write_subresult(sub);283 } else {284 $($ident.abi_write(writer);)+285 }286 }287 }288 };289}290291impl_tuples! {A}292impl_tuples! {A B}293impl_tuples! {A B C}294impl_tuples! {A B C D}295impl_tuples! {A B C D E}296impl_tuples! {A B C D E F}297impl_tuples! {A B C D E F G}298impl_tuples! {A B C D E F G H}299impl_tuples! {A B C D E F G H I}300impl_tuples! {A B C D E F G H I J}crates/evm-coder/src/abi/traits.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/traits.rs
+++ b/crates/evm-coder/src/abi/traits.rs
@@ -10,6 +10,9 @@
/// Signature for Etherium ABI.
const SIGNATURE: SignatureUnit;
+ /// Count of enum variants or struct fields.
+ const FIELDS_COUNT: usize;
+
/// Signature as str.
fn as_str() -> &'static str {
from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
crates/evm-coder/tests/abi_derive_generation.rsdiffbeforeafterboth--- a/crates/evm-coder/tests/abi_derive_generation.rs
+++ b/crates/evm-coder/tests/abi_derive_generation.rs
@@ -164,6 +164,50 @@
}
#[test]
+ fn impl_abi_type_fields_count() {
+ assert_eq!(
+ <TypeStruct1SimpleParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 1
+ );
+ assert_eq!(
+ <TypeStruct1DynamicParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 1
+ );
+ assert_eq!(
+ <TypeStruct2SimpleParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct2DynamicParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct2MixedParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct1DerivedSimpleParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 1
+ );
+ assert_eq!(
+ <TypeStruct2DerivedSimpleParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct1DerivedDynamicParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct2DerivedDynamicParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 2
+ );
+ assert_eq!(
+ <TypeStruct3DerivedMixedParam as evm_coder::abi::AbiType>::FIELDS_COUNT,
+ 3
+ );
+ }
+
+ #[test]
fn impl_abi_type_is_dynamic() {
assert_eq!(
<TypeStruct1SimpleParam as evm_coder::abi::AbiType>::is_dynamic(),