difftreelog
refactor merge sealed::CanBePlacedInVec traits
in: master
7 files changed
crates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/abi_derive.rs
+++ b/crates/evm-coder/procedural/src/abi_derive.rs
@@ -41,7 +41,7 @@
fn impl_can_be_placed_in_vec(ident: &syn::Ident) -> proc_macro2::TokenStream {
quote! {
- impl ::evm_coder::abi::sealed::CanBePlacedInVec for #ident {}
+ impl ::evm_coder::sealed::CanBePlacedInVec for #ident {}
}
}
crates/evm-coder/src/abi/impls.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/impls.rs
+++ b/crates/evm-coder/src/abi/impls.rs
@@ -1,8 +1,8 @@
use crate::{
+ custom_signature::SignatureUnit,
execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},
+ make_signature, sealed,
types::*,
- make_signature,
- custom_signature::SignatureUnit,
};
use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};
use primitive_types::{U256, H160};
crates/evm-coder/src/abi/traits.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/traits.rs
+++ b/crates/evm-coder/src/abi/traits.rs
@@ -22,12 +22,6 @@
fn size() -> usize;
}
-/// Sealed traits.
-pub mod sealed {
- /// Not all types can be placed in vec, i.e `Vec<u8>` is restricted, `bytes` should be used instead
- pub trait CanBePlacedInVec {}
-}
-
/// [`AbiReader`] implements reading of many types.
pub trait AbiRead {
/// Read item from current position, advanding decoder
crates/evm-coder/src/lib.rsdiffbeforeafterboth--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -112,6 +112,15 @@
#[cfg(feature = "stubgen")]
pub mod solidity;
+/// Sealed traits.
+pub mod sealed {
+ /// Not every type should be directly placed in vec.
+ /// Vec encoding is not memory efficient, as every item will be padded
+ /// to 32 bytes.
+ /// Instead you should use specialized types (`bytes` in case of `Vec<u8>`)
+ pub trait CanBePlacedInVec {}
+}
+
/// Solidity type definitions (aliases from solidity name to rust type)
/// To be used in [`solidity_interface`] definitions, to make sure there is no
/// type conflict between Rust code and generated definitions
crates/evm-coder/src/solidity/impls.rsdiffbeforeafterboth1use super::{TypeCollector, SolidityTypeName, SolidityTupleType, sealed};2use crate::types::*;3use core::fmt;45impl sealed::CanBePlacedInVec for uint256 {}6impl sealed::CanBePlacedInVec for string {}7impl sealed::CanBePlacedInVec for address {}89macro_rules! solidity_type_name {10 ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {11 $(12 impl SolidityTypeName for $ty {13 fn solidity_name(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {14 write!(writer, $name)15 }16 fn is_simple() -> bool {17 $simple18 }19 fn solidity_default(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {20 write!(writer, $default)21 }22 }23 )*24 };25}2627solidity_type_name! {28 uint8 => "uint8" true = "0",29 uint32 => "uint32" true = "0",30 uint64 => "uint64" true = "0",31 uint128 => "uint128" true = "0",32 uint256 => "uint256" true = "0",33 bytes4 => "bytes4" true = "bytes4(0)",34 address => "address" true = "0x0000000000000000000000000000000000000000",35 string => "string" false = "\"\"",36 bytes => "bytes" false = "hex\"\"",37 bool => "bool" true = "false",38}3940impl SolidityTypeName for void {41 fn solidity_name(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {42 Ok(())43 }44 fn is_simple() -> bool {45 true46 }47 fn solidity_default(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {48 Ok(())49 }50 fn is_void() -> bool {51 true52 }53}5455impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {56 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {57 T::solidity_name(writer, tc)?;58 write!(writer, "[]")59 }60 fn is_simple() -> bool {61 false62 }63 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {64 write!(writer, "new ")?;65 T::solidity_name(writer, tc)?;66 write!(writer, "[](0)")67 }68}6970macro_rules! count {71 () => (0usize);72 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));73}7475macro_rules! impl_tuples {76 ($($ident:ident)+) => {77 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}78 impl<$($ident: SolidityTypeName + 'static),+> SolidityTupleType for ($($ident,)+) {79 fn names(tc: &TypeCollector) -> Vec<string> {80 let mut collected = Vec::with_capacity(Self::len());81 $({82 let mut out = string::new();83 $ident::solidity_name(&mut out, tc).expect("no fmt error");84 collected.push(out);85 })*;86 collected87 }8889 fn len() -> usize {90 count!($($ident)*)91 }92 }93 impl<$($ident: SolidityTypeName + 'static),+> SolidityTypeName for ($($ident,)+) {94 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {95 write!(writer, "{}", tc.collect_tuple::<Self>())96 }97 fn is_simple() -> bool {98 false99 }100 #[allow(unused_assignments)]101 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {102 write!(writer, "{}(", tc.collect_tuple::<Self>())?;103 let mut first = true;104 $(105 if !first {106 write!(writer, ",")?;107 } else {108 first = false;109 }110 <$ident>::solidity_default(writer, tc)?;111 )*112 write!(writer, ")")113 }114 }115 };116}117118impl_tuples! {A}119impl_tuples! {A B}120impl_tuples! {A B C}121impl_tuples! {A B C D}122impl_tuples! {A B C D E}123impl_tuples! {A B C D E F}124impl_tuples! {A B C D E F G}125impl_tuples! {A B C D E F G H}126impl_tuples! {A B C D E F G H I}127impl_tuples! {A B C D E F G H I J}128129impl sealed::CanBePlacedInVec for Property {}130impl StructCollect for Property {131 fn name() -> String {132 "Property".into()133 }134135 fn declaration() -> String {136 let mut str = String::new();137 writeln!(str, "/// @dev Property struct").unwrap();138 writeln!(str, "struct {} {{", Self::name()).unwrap();139 writeln!(str, "\tstring key;").unwrap();140 writeln!(str, "\tbytes value;").unwrap();141 writeln!(str, "}}").unwrap();142 str143 }144}145146impl SolidityTypeName for Property {147 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {148 write!(writer, "{}", tc.collect_struct::<Self>())149 }150151 fn is_simple() -> bool {152 false153 }154155 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {156 write!(writer, "{}(", tc.collect_struct::<Self>())?;157 address::solidity_default(writer, tc)?;158 write!(writer, ",")?;159 uint256::solidity_default(writer, tc)?;160 write!(writer, ")")161 }162}163164impl SolidityTupleType for Property {165 fn names(tc: &TypeCollector) -> Vec<string> {166 let mut collected = Vec::with_capacity(Self::len());167 {168 let mut out = string::new();169 string::solidity_name(&mut out, tc).expect("no fmt error");170 collected.push(out);171 }172 {173 let mut out = string::new();174 bytes::solidity_name(&mut out, tc).expect("no fmt error");175 collected.push(out);176 }177 collected178 }179180 fn len() -> usize {181 2182 }183}1use super::{TypeCollector, SolidityTypeName, SolidityTupleType};2use crate::{sealed, types::*};3use core::fmt;45macro_rules! solidity_type_name {6 ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {7 $(8 impl SolidityTypeName for $ty {9 fn solidity_name(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {10 write!(writer, $name)11 }12 fn is_simple() -> bool {13 $simple14 }15 fn solidity_default(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {16 write!(writer, $default)17 }18 }19 )*20 };21}2223solidity_type_name! {24 uint8 => "uint8" true = "0",25 uint32 => "uint32" true = "0",26 uint64 => "uint64" true = "0",27 uint128 => "uint128" true = "0",28 uint256 => "uint256" true = "0",29 bytes4 => "bytes4" true = "bytes4(0)",30 address => "address" true = "0x0000000000000000000000000000000000000000",31 string => "string" false = "\"\"",32 bytes => "bytes" false = "hex\"\"",33 bool => "bool" true = "false",34}3536impl SolidityTypeName for void {37 fn solidity_name(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {38 Ok(())39 }40 fn is_simple() -> bool {41 true42 }43 fn solidity_default(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {44 Ok(())45 }46 fn is_void() -> bool {47 true48 }49}5051impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {52 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {53 T::solidity_name(writer, tc)?;54 write!(writer, "[]")55 }56 fn is_simple() -> bool {57 false58 }59 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {60 write!(writer, "new ")?;61 T::solidity_name(writer, tc)?;62 write!(writer, "[](0)")63 }64}6566macro_rules! count {67 () => (0usize);68 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));69}7071macro_rules! impl_tuples {72 ($($ident:ident)+) => {73 impl<$($ident: SolidityTypeName + 'static),+> SolidityTupleType for ($($ident,)+) {74 fn names(tc: &TypeCollector) -> Vec<string> {75 let mut collected = Vec::with_capacity(Self::len());76 $({77 let mut out = string::new();78 $ident::solidity_name(&mut out, tc).expect("no fmt error");79 collected.push(out);80 })*;81 collected82 }8384 fn len() -> usize {85 count!($($ident)*)86 }87 }88 impl<$($ident: SolidityTypeName + 'static),+> SolidityTypeName for ($($ident,)+) {89 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {90 write!(writer, "{}", tc.collect_tuple::<Self>())91 }92 fn is_simple() -> bool {93 false94 }95 #[allow(unused_assignments)]96 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {97 write!(writer, "{}(", tc.collect_tuple::<Self>())?;98 let mut first = true;99 $(100 if !first {101 write!(writer, ",")?;102 } else {103 first = false;104 }105 <$ident>::solidity_default(writer, tc)?;106 )*107 write!(writer, ")")108 }109 }110 };111}112113impl_tuples! {A}114impl_tuples! {A B}115impl_tuples! {A B C}116impl_tuples! {A B C D}117impl_tuples! {A B C D E}118impl_tuples! {A B C D E F}119impl_tuples! {A B C D E F G}120impl_tuples! {A B C D E F G H}121impl_tuples! {A B C D E F G H I}122impl_tuples! {A B C D E F G H I J}123124impl sealed::CanBePlacedInVec for Property {}125impl StructCollect for Property {126 fn name() -> String {127 "Property".into()128 }129130 fn declaration() -> String {131 let mut str = String::new();132 writeln!(str, "/// @dev Property struct").unwrap();133 writeln!(str, "struct {} {{", Self::name()).unwrap();134 writeln!(str, "\tstring key;").unwrap();135 writeln!(str, "\tbytes value;").unwrap();136 writeln!(str, "}}").unwrap();137 str138 }139}140141impl SolidityTypeName for Property {142 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {143 write!(writer, "{}", tc.collect_struct::<Self>())144 }145146 fn is_simple() -> bool {147 false148 }149150 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {151 write!(writer, "{}(", tc.collect_struct::<Self>())?;152 address::solidity_default(writer, tc)?;153 write!(writer, ",")?;154 uint256::solidity_default(writer, tc)?;155 write!(writer, ")")156 }157}158159impl SolidityTupleType for Property {160 fn names(tc: &TypeCollector) -> Vec<string> {161 let mut collected = Vec::with_capacity(Self::len());162 {163 let mut out = string::new();164 string::solidity_name(&mut out, tc).expect("no fmt error");165 collected.push(out);166 }167 {168 let mut out = string::new();169 bytes::solidity_name(&mut out, tc).expect("no fmt error");170 collected.push(out);171 }172 collected173 }174175 fn len() -> usize {176 2177 }178}crates/evm-coder/src/solidity/traits.rsdiffbeforeafterboth--- a/crates/evm-coder/src/solidity/traits.rs
+++ b/crates/evm-coder/src/solidity/traits.rs
@@ -1,14 +1,6 @@
use super::TypeCollector;
use core::fmt;
-pub mod sealed {
- /// Not every type should be directly placed in vec.
- /// Vec encoding is not memory efficient, as every item will be padded
- /// to 32 bytes.
- /// Instead you should use specialized types (`bytes` in case of `Vec<u8>`)
- pub trait CanBePlacedInVec {}
-}
-
pub trait StructCollect: 'static {
/// Structure name.
fn name() -> String;
pallets/common/src/eth.rsdiffbeforeafterboth--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -156,9 +156,6 @@
}
#[cfg(feature = "stubgen")]
-impl ::evm_coder::solidity::sealed::CanBePlacedInVec for EthCrossAccount {}
-
-#[cfg(feature = "stubgen")]
impl ::evm_coder::solidity::SolidityTupleType for EthCrossAccount {
fn names(tc: &::evm_coder::solidity::TypeCollector) -> Vec<String> {
let mut collected =