git.delta.rocks / unique-network / refs/commits / fe70dfc798ca

difftreelog

refactor decouple make_signature from constant itself

Yaroslav Bolyukin2022-11-02parent: #773312d.patch.diff
in: master

9 files changed

modifiedcrates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder/procedural/src/solidity_interface.rs
+++ b/crates/evm-coder/procedural/src/solidity_interface.rs
@@ -721,7 +721,7 @@
 		for arg in self.args.iter().filter(|a| !a.is_special()) {
 			has_params = true;
 			let ty = &arg.ty;
-			args.extend(quote! {nameof(#ty)});
+			args.extend(quote! {nameof(<#ty>::SIGNATURE)});
 			args.extend(quote! {fixed(",")})
 		}
 
modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -428,7 +428,7 @@
 }
 
 impl<R: Signature> Signature for Vec<R> {
-	const SIGNATURE: SignatureUnit = make_signature!(new nameof(R) fixed("[]"));
+	const SIGNATURE: SignatureUnit = make_signature!(new nameof(R::SIGNATURE) fixed("[]"));
 }
 
 impl sealed::CanBePlacedInVec for EthCrossAccount {}
@@ -524,7 +524,7 @@
 		{
 			const SIGNATURE: SignatureUnit = make_signature!(
 				new fixed("(")
-				$(nameof($ident) fixed(","))+
+				$(nameof(<$ident>::SIGNATURE) fixed(","))+
 				shift_left(1)
 				fixed(")")
 			);
modifiedcrates/evm-coder/src/custom_signature.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/custom_signature.rs
+++ b/crates/evm-coder/src/custom_signature.rs
@@ -9,37 +9,33 @@
 //! #### Example
 //! ```
 //! use std::str::from_utf8;
-//! use evm_coder::make_signature;
-//! use evm_coder::custom_signature::{
-//! 	SignatureUnit,
-//! 	SIGNATURE_SIZE_LIMIT
-//! };
+//! use evm_coder::{custom_signature::SignatureUnit, make_signature};
 //!
 //! // Create trait for our signature
 //! trait SoliditySignature {
-//!		const SIGNATURE: SignatureUnit;
+//!     const SIGNATURE: SignatureUnit;
 //!
-//!		fn name() -> &'static str {
-//!			from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
-//!		}
-//!	}
+//!     fn name() -> &'static str {
+//!         from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
+//!     }
+//! }
 //!
 //! // Make signatures for some types
-//!	impl SoliditySignature for u8 {
-//!		make_signature!(new fixed("uint8"));
-//!	}
-//!	impl SoliditySignature for u32 {
-//!		make_signature!(new fixed("uint32"));
-//!	}
-//!	impl<T: SoliditySignature> SoliditySignature for Vec<T> {
-//!		make_signature!(new nameof(T) fixed("[]"));
-//!	}
-//!	impl<A: SoliditySignature, B: SoliditySignature> SoliditySignature for (A, B) {
-//!		make_signature!(new fixed("(") nameof(A) fixed(",") nameof(B) fixed(")"));
-//!	}
-//!	impl<A: SoliditySignature> SoliditySignature for (A,) {
-//!		make_signature!(new fixed("(") nameof(A) fixed(",") shift_left(1) fixed(")"));
-//!	}
+//! impl SoliditySignature for u8 {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
+//! }
+//! impl SoliditySignature for u32 {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint32"));
+//! }
+//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
+//! }
+//! impl<A: SoliditySignature, B: SoliditySignature> SoliditySignature for (A, B) {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") nameof(B::SIGNATURE) fixed(")"));
+//! }
+//! impl<A: SoliditySignature> SoliditySignature for (A,) {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") shift_left(1) fixed(")"));
+//! }
 //!
 //! assert_eq!(u8::name(), "uint8");
 //! assert_eq!(<Vec<u8>>::name(), "uint8[]");
@@ -52,28 +48,23 @@
 //! #### Example
 //! ```
 //! use core::str::from_utf8;
-//! use evm_coder::{
-//!		make_signature,
-//!		custom_signature::{
-//!			SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
-//!		},
-//!	};
+//! use evm_coder::{custom_signature::SignatureUnit, make_signature};
 //! // Trait for our signature
 //! trait SoliditySignature {
-//!		const SIGNATURE: SignatureUnit;
+//!     const SIGNATURE: SignatureUnit;
 //!
-//!		fn name() -> &'static str {
-//!			from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
-//!		}
-//!	}
+//!     fn name() -> &'static str {
+//!         from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
+//!     }
+//! }
 //!
 //! // Make signatures for some types
-//!	impl SoliditySignature for u8 {
-//!		make_signature!(new fixed("uint8"));
-//!	}
-//!	impl<T: SoliditySignature> SoliditySignature for Vec<T> {
-//!		make_signature!(new nameof(T) fixed("[]"));
-//!	}
+//! impl SoliditySignature for u8 {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
+//! }
+//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {
+//!     const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
+//! }
 //! ```
 
 /// The maximum length of the signature.
@@ -134,8 +125,23 @@
 	(@size; fixed($expr:expr) $($tt:tt)*) => {
 		$expr.len() + $crate::make_signature!(@size; $($tt)*)
 	};
-	(@size; nameof($expr:ty) $($tt:tt)*) => {
-		<$expr>::SIGNATURE.len + $crate::make_signature!(@size; $($tt)*)
+	(@size; nameof($expr:expr) $($tt:tt)*) => {
+		$expr.len + $crate::make_signature!(@size; $($tt)*)
+	};
+	(@size; numof($expr:expr) $($tt:tt)*) => {
+		{
+			let mut out = 0;
+			let mut v = $expr;
+			if v == 0 {
+				out = 1;
+			} else {
+				while v > 0 {
+					out += 1;
+					v /= 10;
+				}
+			}
+			out
+		} + $crate::make_signature!(@size; $($tt)*)
 	};
 	(@size; shift_left($expr:expr) $($tt:tt)*) => {
 		$crate::make_signature!(@size; $($tt)*) - $expr
@@ -150,12 +156,41 @@
 		}
 		$crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
 	};
-	(@data($dst:ident, $dst_offset:ident); nameof($expr:ty) $($tt:tt)*) => {
+	(@data($dst:ident, $dst_offset:ident); nameof($expr:expr) $($tt:tt)*) => {
 		{
-			$crate::make_signature!(@copy(&<$expr>::SIGNATURE.data, $dst, <$expr>::SIGNATURE.len, $dst_offset));
+			$crate::make_signature!(@copy(&$expr.data, $dst, $expr.len, $dst_offset));
 		}
 		$crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
 	};
+	(@data($dst:ident, $dst_offset:ident); numof($expr:expr) $($tt:tt)*) => {
+		{
+			let mut v = $expr;
+			let mut need_to_swap = 0;
+			if v == 0 {
+				$dst[$dst_offset] = b'0';
+				$dst_offset += 1;
+			} else {
+				while v > 0 {
+					let n = (v % 10) as u8;
+					$dst[$dst_offset] = b'0' + n;
+					v /= 10;
+					need_to_swap += 1;
+					$dst_offset += 1;
+				}
+			}
+			let mut i = 0;
+			#[allow(clippy::manual_swap)]
+			while i < need_to_swap / 2 {
+				let a = $dst_offset - i - 1;
+				let b = $dst_offset - need_to_swap + i;
+				let v = $dst[a];
+				$dst[a] = $dst[b];
+				$dst[b] = v;
+				i += 1;
+			}
+		}
+		$crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
+	};
 	(@data($dst:ident, $dst_offset:ident); shift_left($expr:expr) $($tt:tt)*) => {
 		$dst_offset -= $expr;
 		$crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
@@ -181,34 +216,38 @@
 	use super::{SIGNATURE_SIZE_LIMIT, SignatureUnit};
 
 	trait Name {
-		const SIGNATURE: SignatureUnit;
+		const NAME: SignatureUnit;
 
 		fn name() -> &'static str {
-			from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
+			from_utf8(&Self::NAME.data[..Self::NAME.len]).expect("bad utf-8")
 		}
 	}
 
 	impl Name for u8 {
-		const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
+		const NAME: SignatureUnit = make_signature!(new fixed("uint8"));
 	}
 	impl Name for u32 {
-		const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint32"));
+		const NAME: SignatureUnit = make_signature!(new fixed("uint32"));
 	}
 	impl<T: Name> Name for Vec<T> {
-		const SIGNATURE: SignatureUnit = make_signature!(new nameof(T) fixed("[]"));
+		const NAME: SignatureUnit = make_signature!(new nameof(T::NAME) fixed("[]"));
 	}
 	impl<A: Name, B: Name> Name for (A, B) {
-		const SIGNATURE: SignatureUnit =
-			make_signature!(new fixed("(") nameof(A) fixed(",") nameof(B) fixed(")"));
+		const NAME: SignatureUnit =
+			make_signature!(new fixed("(") nameof(A::NAME) fixed(",") nameof(B::NAME) fixed(")"));
 	}
 	impl<A: Name> Name for (A,) {
-		const SIGNATURE: SignatureUnit =
-			make_signature!(new fixed("(") nameof(A) fixed(",") shift_left(1) fixed(")"));
+		const NAME: SignatureUnit =
+			make_signature!(new fixed("(") nameof(A::NAME) fixed(",") shift_left(1) fixed(")"));
+	}
+	impl<A: Name, const SIZE: usize> Name for [A; SIZE] {
+		const NAME: SignatureUnit =
+			make_signature!(new nameof(A::NAME) fixed("[") numof(SIZE) fixed("]"));
 	}
 
 	struct MaxSize();
 	impl Name for MaxSize {
-		const SIGNATURE: SignatureUnit = SignatureUnit {
+		const NAME: SignatureUnit = SignatureUnit {
 			data: [b'!'; SIGNATURE_SIZE_LIMIT],
 			len: SIGNATURE_SIZE_LIMIT,
 		};
@@ -273,6 +312,13 @@
 	}
 
 	#[test]
+	fn num() {
+		assert_eq!(<[u8; 0]>::name(), "uint8[0]");
+		assert_eq!(<[u8; 1234]>::name(), "uint8[1234]");
+		assert_eq!(<[u8; 12345]>::name(), "uint8[12345]");
+	}
+
+	#[test]
 	fn over_max_size() {
 		let t = trybuild::TestCases::new();
 		t.compile_fail("tests/build_failed/custom_signature_over_max_size.rs");
modifiedcrates/evm-coder/tests/build_failed/custom_signature_over_max_size.rsdiffbeforeafterboth
--- a/crates/evm-coder/tests/build_failed/custom_signature_over_max_size.rs
+++ b/crates/evm-coder/tests/build_failed/custom_signature_over_max_size.rs
@@ -15,7 +15,8 @@
 }
 
 impl<T: Name> Name for Vec<T> {
-	evm_coder::make_signature!(new nameof(T) fixed("[]"));
+	const SIGNATURE: SignatureUnit =
+		evm_coder::make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
 }
 
 struct MaxSize();
modifiedcrates/evm-coder/tests/build_failed/custom_signature_over_max_size.stderrdiffbeforeafterboth
--- a/crates/evm-coder/tests/build_failed/custom_signature_over_max_size.stderr
+++ b/crates/evm-coder/tests/build_failed/custom_signature_over_max_size.stderr
@@ -1,18 +1,28 @@
+warning: unused import: `make_signature`
+ --> tests/build_failed/custom_signature_over_max_size.rs:5:2
+  |
+5 |     make_signature,
+  |     ^^^^^^^^^^^^^^
+  |
+  = note: `#[warn(unused_imports)]` on by default
+
 error: any use of this value will cause an error
-  --> tests/build_failed/custom_signature_over_max_size.rs:18:2
+  --> tests/build_failed/custom_signature_over_max_size.rs:19:3
    |
-18 |     evm_coder::make_signature!(new nameof(T) fixed("[]"));
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 256 but the index is 256
+18 |     const SIGNATURE: SignatureUnit =
+   |     ------------------------------
+19 |         evm_coder::make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 256 but the index is 256
    |
    = note: `#[deny(const_err)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
-   = note: this error originates in the macro `make_signature` which comes from the expansion of the macro `evm_coder::make_signature` (in Nightly builds, run with -Z macro-backtrace for more info)
+   = note: this error originates in the macro `$crate::make_signature` which comes from the expansion of the macro `evm_coder::make_signature` (in Nightly builds, run with -Z macro-backtrace for more info)
 
 error: any use of this value will cause an error
-  --> tests/build_failed/custom_signature_over_max_size.rs:29:29
+  --> tests/build_failed/custom_signature_over_max_size.rs:30:29
    |
-29 | const NAME: SignatureUnit = <Vec<MaxSize>>::SIGNATURE;
+30 | const NAME: SignatureUnit = <Vec<MaxSize>>::SIGNATURE;
    | -------------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
modifiedcrates/evm-coder/tests/conditional_is.rsdiffbeforeafterboth
--- a/crates/evm-coder/tests/conditional_is.rs
+++ b/crates/evm-coder/tests/conditional_is.rs
@@ -1,11 +1,4 @@
 use evm_coder::{types::*, solidity_interface, execution::Result};
-use evm_coder::{
-	make_signature,
-	custom_signature::{
-		SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
-	},
-	types::Signature,
-};
 
 pub struct Contract(bool);
 
modifiedcrates/evm-coder/tests/generics.rsdiffbeforeafterboth
--- a/crates/evm-coder/tests/generics.rs
+++ b/crates/evm-coder/tests/generics.rs
@@ -16,12 +16,6 @@
 
 use std::marker::PhantomData;
 use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
-use evm_coder::{
-	make_signature,
-	custom_signature::{
-		SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
-	},
-};
 
 pub struct Generic<T>(PhantomData<T>);
 
modifiedcrates/evm-coder/tests/random.rsdiffbeforeafterboth
before · crates/evm-coder/tests/random.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![allow(dead_code)] // This test only checks that macros is not panicking1819use evm_coder::{ToLog, execution::Result, solidity_interface, types::*, solidity, weight};20use evm_coder::{21	make_signature,22	custom_signature::{23		SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,24	},25	types::Signature,26};2728pub struct Impls;2930#[solidity_interface(name = OurInterface)]31impl Impls {32	fn fn_a(&self, _input: uint256) -> Result<bool> {33		unreachable!()34	}35}3637#[solidity_interface(name = OurInterface1)]38impl Impls {39	fn fn_b(&self, _input: uint128) -> Result<uint32> {40		unreachable!()41	}42}4344#[derive(ToLog)]45enum OurEvents {46	Event1 {47		field1: uint32,48	},49	Event2 {50		field1: uint32,51		#[indexed]52		field2: uint32,53	},54}5556#[solidity_interface(57	name = OurInterface2,58	is(OurInterface),59	inline_is(OurInterface1),60	events(OurEvents)61)]62impl Impls {63	#[solidity(rename_selector = "fnK")]64	fn fn_c(&self, _input: uint32) -> Result<uint8> {65		unreachable!()66	}67	fn fn_d(&self, _value: uint32) -> Result<uint32> {68		unreachable!()69	}7071	fn caller_sensitive(&self, _caller: caller) -> Result<uint8> {72		unreachable!()73	}74	fn payable(&mut self, _value: value) -> Result<uint8> {75		unreachable!()76	}7778	#[weight(*_weight)]79	fn with_weight(&self, _weight: uint64) -> Result<void> {80		unreachable!()81	}8283	/// Doccoment example84	fn with_doc(&self) -> Result<void> {85		unreachable!()86	}87}8889#[solidity_interface(90	name = ValidSelector,91	expect_selector = 0x00000000,92)]93impl Impls {}
after · crates/evm-coder/tests/random.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![allow(dead_code)] // This test only checks that macros is not panicking1819use evm_coder::{ToLog, execution::Result, solidity_interface, types::*, solidity, weight};20use evm_coder::{types::Signature};2122pub struct Impls;2324#[solidity_interface(name = OurInterface)]25impl Impls {26	fn fn_a(&self, _input: uint256) -> Result<bool> {27		unreachable!()28	}29}3031#[solidity_interface(name = OurInterface1)]32impl Impls {33	fn fn_b(&self, _input: uint128) -> Result<uint32> {34		unreachable!()35	}36}3738#[derive(ToLog)]39enum OurEvents {40	Event1 {41		field1: uint32,42	},43	Event2 {44		field1: uint32,45		#[indexed]46		field2: uint32,47	},48}4950#[solidity_interface(51	name = OurInterface2,52	is(OurInterface),53	inline_is(OurInterface1),54	events(OurEvents)55)]56impl Impls {57	#[solidity(rename_selector = "fnK")]58	fn fn_c(&self, _input: uint32) -> Result<uint8> {59		unreachable!()60	}61	fn fn_d(&self, _value: uint32) -> Result<uint32> {62		unreachable!()63	}6465	fn caller_sensitive(&self, _caller: caller) -> Result<uint8> {66		unreachable!()67	}68	fn payable(&mut self, _value: value) -> Result<uint8> {69		unreachable!()70	}7172	#[weight(*_weight)]73	fn with_weight(&self, _weight: uint64) -> Result<void> {74		unreachable!()75	}7677	/// Doccoment example78	fn with_doc(&self) -> Result<void> {79		unreachable!()80	}81}8283#[solidity_interface(84	name = ValidSelector,85	expect_selector = 0x00000000,86)]87impl Impls {}
modifiedcrates/evm-coder/tests/solidity_generation.rsdiffbeforeafterboth
--- a/crates/evm-coder/tests/solidity_generation.rs
+++ b/crates/evm-coder/tests/solidity_generation.rs
@@ -15,13 +15,7 @@
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
 use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
-use evm_coder::{
-	make_signature,
-	custom_signature::{
-		SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
-	},
-	types::Signature,
-};
+use evm_coder::{types::Signature};
 
 pub struct ERC20;