git.delta.rocks / unique-network / refs/commits / 81e4f2e24fd5

difftreelog

source

crates/evm-coder/src/solidity/impls.rs3.6 KiBsourcehistory
1use super::{TypeCollector, SolidityTypeName, SolidityType, StructCollect};2use crate::{sealed, types::*};3use core::fmt;4use primitive_types::{U256, H160};56macro_rules! solidity_type_name {7    ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {8        $(9            impl SolidityTypeName for $ty {10                fn solidity_name(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {11                    write!(writer, $name)12                }13				fn is_simple() -> bool {14					$simple15				}16				fn solidity_default(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {17					write!(writer, $default)18				}19            }2021			impl StructCollect for $ty {22				fn name() -> String {23					$name.to_string()24				}2526				fn declaration() -> String {27					String::default()28				}29			}30        )*31    };32}3334solidity_type_name! {35	u8 => "uint8" true = "0",36	u32 => "uint32" true = "0",37	u64 => "uint64" true = "0",38	u128 => "uint128" true = "0",39	U256 => "uint256" true = "0",40	bytes4 => "bytes4" true = "bytes4(0)",41	H160 => "address" true = "0x0000000000000000000000000000000000000000",42	string => "string" false = "\"\"",43	bytes => "bytes" false = "hex\"\"",44	bool => "bool" true = "false",45}4647impl SolidityTypeName for void {48	fn solidity_name(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {49		Ok(())50	}51	fn is_simple() -> bool {52		true53	}54	fn solidity_default(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {55		Ok(())56	}57	fn is_void() -> bool {58		true59	}60}6162impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {63	fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {64		T::solidity_name(writer, tc)?;65		write!(writer, "[]")66	}67	fn is_simple() -> bool {68		false69	}70	fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {71		write!(writer, "new ")?;72		T::solidity_name(writer, tc)?;73		write!(writer, "[](0)")74	}75}7677impl<T: StructCollect + sealed::CanBePlacedInVec> StructCollect for Vec<T> {78	fn name() -> String {79		<T as StructCollect>::name() + "[]"80	}8182	fn declaration() -> String {83		unimplemented!("Vectors have not declarations.")84	}85}8687macro_rules! count {88    () => (0usize);89    ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));90}9192macro_rules! impl_tuples {93	($($ident:ident)+) => {94		impl<$($ident: SolidityTypeName + 'static),+> SolidityType for ($($ident,)+) {95			fn names(tc: &TypeCollector) -> Vec<string> {96				let mut collected = Vec::with_capacity(Self::len());97				$({98					let mut out = string::new();99					$ident::solidity_name(&mut out, tc).expect("no fmt error");100					collected.push(out);101				})*;102				collected103			}104105			fn len() -> usize {106				count!($($ident)*)107			}108		}109		impl<$($ident: SolidityTypeName + 'static),+> SolidityTypeName for ($($ident,)+) {110			fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {111				write!(writer, "{}", tc.collect_tuple::<Self>())112			}113			fn is_simple() -> bool {114				false115			}116			#[allow(unused_assignments)]117			fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {118				write!(writer, "{}(", tc.collect_tuple::<Self>())?;119				let mut first = true;120				$(121					if !first {122						write!(writer, ",")?;123					} else {124						first = false;125					}126					<$ident>::solidity_default(writer, tc)?;127				)*128				write!(writer, ")")129			}130		}131	};132}133134impl_tuples! {A}135impl_tuples! {A B}136impl_tuples! {A B C}137impl_tuples! {A B C D}138impl_tuples! {A B C D E}139impl_tuples! {A B C D E F}140impl_tuples! {A B C D E F G}141impl_tuples! {A B C D E F G H}142impl_tuples! {A B C D E F G H I}143impl_tuples! {A B C D E F G H I J}