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
721 for arg in self.args.iter().filter(|a| !a.is_special()) {721 for arg in self.args.iter().filter(|a| !a.is_special()) {
722 has_params = true;722 has_params = true;
723 let ty = &arg.ty;723 let ty = &arg.ty;
724 args.extend(quote! {nameof(#ty)});724 args.extend(quote! {nameof(<#ty>::SIGNATURE)});
725 args.extend(quote! {fixed(",")})725 args.extend(quote! {fixed(",")})
726 }726 }
727727
modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
428}428}
429429
430impl<R: Signature> Signature for Vec<R> {430impl<R: Signature> Signature for Vec<R> {
431 const SIGNATURE: SignatureUnit = make_signature!(new nameof(R) fixed("[]"));431 const SIGNATURE: SignatureUnit = make_signature!(new nameof(R::SIGNATURE) fixed("[]"));
432}432}
433433
434impl sealed::CanBePlacedInVec for EthCrossAccount {}434impl sealed::CanBePlacedInVec for EthCrossAccount {}
524 {524 {
525 const SIGNATURE: SignatureUnit = make_signature!(525 const SIGNATURE: SignatureUnit = make_signature!(
526 new fixed("(")526 new fixed("(")
527 $(nameof($ident) fixed(","))+527 $(nameof(<$ident>::SIGNATURE) fixed(","))+
528 shift_left(1)528 shift_left(1)
529 fixed(")")529 fixed(")")
530 );530 );
modifiedcrates/evm-coder/src/custom_signature.rsdiffbeforeafterboth
9//! #### Example9//! #### Example
10//! ```10//! ```
11//! use std::str::from_utf8;11//! use std::str::from_utf8;
12//! use evm_coder::make_signature;
13//! use evm_coder::custom_signature::{12//! use evm_coder::{custom_signature::SignatureUnit, make_signature};
14//! SignatureUnit,
15//! SIGNATURE_SIZE_LIMIT
16//! };
17//!13//!
18//! // Create trait for our signature14//! // Create trait for our signature
19//! trait SoliditySignature {15//! trait SoliditySignature {
20//! const SIGNATURE: SignatureUnit;16//! const SIGNATURE: SignatureUnit;
21//!17//!
22//! fn name() -> &'static str {18//! fn name() -> &'static str {
23//! from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")19//! from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
24//! }20//! }
25//! }21//! }
26//!22//!
27//! // Make signatures for some types23//! // Make signatures for some types
28//! impl SoliditySignature for u8 {24//! impl SoliditySignature for u8 {
29//! make_signature!(new fixed("uint8"));25//! const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
30//! }26//! }
31//! impl SoliditySignature for u32 {27//! impl SoliditySignature for u32 {
32//! make_signature!(new fixed("uint32"));28//! const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint32"));
33//! }29//! }
34//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {30//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {
35//! make_signature!(new nameof(T) fixed("[]"));31//! const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
36//! }32//! }
37//! impl<A: SoliditySignature, B: SoliditySignature> SoliditySignature for (A, B) {33//! impl<A: SoliditySignature, B: SoliditySignature> SoliditySignature for (A, B) {
38//! make_signature!(new fixed("(") nameof(A) fixed(",") nameof(B) fixed(")"));34//! const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") nameof(B::SIGNATURE) fixed(")"));
39//! }35//! }
40//! impl<A: SoliditySignature> SoliditySignature for (A,) {36//! impl<A: SoliditySignature> SoliditySignature for (A,) {
41//! make_signature!(new fixed("(") nameof(A) fixed(",") shift_left(1) fixed(")"));37//! const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") shift_left(1) fixed(")"));
42//! }38//! }
43//!39//!
44//! assert_eq!(u8::name(), "uint8");40//! assert_eq!(u8::name(), "uint8");
45//! assert_eq!(<Vec<u8>>::name(), "uint8[]");41//! assert_eq!(<Vec<u8>>::name(), "uint8[]");
52//! #### Example48//! #### Example
53//! ```49//! ```
54//! use core::str::from_utf8;50//! use core::str::from_utf8;
55//! use evm_coder::{51//! use evm_coder::{custom_signature::SignatureUnit, make_signature};
56//! make_signature,
57//! custom_signature::{
58//! SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
59//! },
60//! };
61//! // Trait for our signature52//! // Trait for our signature
62//! trait SoliditySignature {53//! trait SoliditySignature {
63//! const SIGNATURE: SignatureUnit;54//! const SIGNATURE: SignatureUnit;
64//!55//!
65//! fn name() -> &'static str {56//! fn name() -> &'static str {
66//! from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")57//! from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
67//! }58//! }
68//! }59//! }
69//!60//!
70//! // Make signatures for some types61//! // Make signatures for some types
71//! impl SoliditySignature for u8 {62//! impl SoliditySignature for u8 {
72//! make_signature!(new fixed("uint8"));63//! const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
73//! }64//! }
74//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {65//! impl<T: SoliditySignature> SoliditySignature for Vec<T> {
75//! make_signature!(new nameof(T) fixed("[]"));66//! const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
76//! }67//! }
77//! ```68//! ```
7869
79/// The maximum length of the signature.70/// The maximum length of the signature.
134 (@size; fixed($expr:expr) $($tt:tt)*) => {125 (@size; fixed($expr:expr) $($tt:tt)*) => {
135 $expr.len() + $crate::make_signature!(@size; $($tt)*)126 $expr.len() + $crate::make_signature!(@size; $($tt)*)
136 };127 };
137 (@size; nameof($expr:ty) $($tt:tt)*) => {128 (@size; nameof($expr:expr) $($tt:tt)*) => {
138 <$expr>::SIGNATURE.len + $crate::make_signature!(@size; $($tt)*)129 $expr.len + $crate::make_signature!(@size; $($tt)*)
139 };130 };
131 (@size; numof($expr:expr) $($tt:tt)*) => {
132 {
133 let mut out = 0;
134 let mut v = $expr;
135 if v == 0 {
136 out = 1;
137 } else {
138 while v > 0 {
139 out += 1;
140 v /= 10;
141 }
142 }
143 out
144 } + $crate::make_signature!(@size; $($tt)*)
145 };
140 (@size; shift_left($expr:expr) $($tt:tt)*) => {146 (@size; shift_left($expr:expr) $($tt:tt)*) => {
141 $crate::make_signature!(@size; $($tt)*) - $expr147 $crate::make_signature!(@size; $($tt)*) - $expr
142 };148 };
150 }156 }
151 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)157 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
152 };158 };
153 (@data($dst:ident, $dst_offset:ident); nameof($expr:ty) $($tt:tt)*) => {159 (@data($dst:ident, $dst_offset:ident); nameof($expr:expr) $($tt:tt)*) => {
154 {160 {
155 $crate::make_signature!(@copy(&<$expr>::SIGNATURE.data, $dst, <$expr>::SIGNATURE.len, $dst_offset));161 $crate::make_signature!(@copy(&$expr.data, $dst, $expr.len, $dst_offset));
156 }162 }
157 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)163 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
158 };164 };
165 (@data($dst:ident, $dst_offset:ident); numof($expr:expr) $($tt:tt)*) => {
166 {
167 let mut v = $expr;
168 let mut need_to_swap = 0;
169 if v == 0 {
170 $dst[$dst_offset] = b'0';
171 $dst_offset += 1;
172 } else {
173 while v > 0 {
174 let n = (v % 10) as u8;
175 $dst[$dst_offset] = b'0' + n;
176 v /= 10;
177 need_to_swap += 1;
178 $dst_offset += 1;
179 }
180 }
181 let mut i = 0;
182 #[allow(clippy::manual_swap)]
183 while i < need_to_swap / 2 {
184 let a = $dst_offset - i - 1;
185 let b = $dst_offset - need_to_swap + i;
186 let v = $dst[a];
187 $dst[a] = $dst[b];
188 $dst[b] = v;
189 i += 1;
190 }
191 }
192 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
193 };
159 (@data($dst:ident, $dst_offset:ident); shift_left($expr:expr) $($tt:tt)*) => {194 (@data($dst:ident, $dst_offset:ident); shift_left($expr:expr) $($tt:tt)*) => {
160 $dst_offset -= $expr;195 $dst_offset -= $expr;
161 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)196 $crate::make_signature!(@data($dst, $dst_offset); $($tt)*)
181 use super::{SIGNATURE_SIZE_LIMIT, SignatureUnit};216 use super::{SIGNATURE_SIZE_LIMIT, SignatureUnit};
182217
183 trait Name {218 trait Name {
184 const SIGNATURE: SignatureUnit;219 const NAME: SignatureUnit;
185220
186 fn name() -> &'static str {221 fn name() -> &'static str {
187 from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")222 from_utf8(&Self::NAME.data[..Self::NAME.len]).expect("bad utf-8")
188 }223 }
189 }224 }
190225
191 impl Name for u8 {226 impl Name for u8 {
192 const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));227 const NAME: SignatureUnit = make_signature!(new fixed("uint8"));
193 }228 }
194 impl Name for u32 {229 impl Name for u32 {
195 const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint32"));230 const NAME: SignatureUnit = make_signature!(new fixed("uint32"));
196 }231 }
197 impl<T: Name> Name for Vec<T> {232 impl<T: Name> Name for Vec<T> {
198 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T) fixed("[]"));233 const NAME: SignatureUnit = make_signature!(new nameof(T::NAME) fixed("[]"));
199 }234 }
200 impl<A: Name, B: Name> Name for (A, B) {235 impl<A: Name, B: Name> Name for (A, B) {
201 const SIGNATURE: SignatureUnit =236 const NAME: SignatureUnit =
202 make_signature!(new fixed("(") nameof(A) fixed(",") nameof(B) fixed(")"));237 make_signature!(new fixed("(") nameof(A::NAME) fixed(",") nameof(B::NAME) fixed(")"));
203 }238 }
204 impl<A: Name> Name for (A,) {239 impl<A: Name> Name for (A,) {
205 const SIGNATURE: SignatureUnit =240 const NAME: SignatureUnit =
206 make_signature!(new fixed("(") nameof(A) fixed(",") shift_left(1) fixed(")"));241 make_signature!(new fixed("(") nameof(A::NAME) fixed(",") shift_left(1) fixed(")"));
207 }242 }
243 impl<A: Name, const SIZE: usize> Name for [A; SIZE] {
244 const NAME: SignatureUnit =
245 make_signature!(new nameof(A::NAME) fixed("[") numof(SIZE) fixed("]"));
246 }
208247
209 struct MaxSize();248 struct MaxSize();
210 impl Name for MaxSize {249 impl Name for MaxSize {
211 const SIGNATURE: SignatureUnit = SignatureUnit {250 const NAME: SignatureUnit = SignatureUnit {
212 data: [b'!'; SIGNATURE_SIZE_LIMIT],251 data: [b'!'; SIGNATURE_SIZE_LIMIT],
213 len: SIGNATURE_SIZE_LIMIT,252 len: SIGNATURE_SIZE_LIMIT,
214 };253 };
272 assert_eq!(<(u32,)>::name(), "(uint32)");311 assert_eq!(<(u32,)>::name(), "(uint32)");
273 }312 }
313
314 #[test]
315 fn num() {
316 assert_eq!(<[u8; 0]>::name(), "uint8[0]");
317 assert_eq!(<[u8; 1234]>::name(), "uint8[1234]");
318 assert_eq!(<[u8; 12345]>::name(), "uint8[12345]");
319 }
274320
275 #[test]321 #[test]
276 fn over_max_size() {322 fn over_max_size() {
modifiedcrates/evm-coder/tests/build_failed/custom_signature_over_max_size.rsdiffbeforeafterboth
15}15}
1616
17impl<T: Name> Name for Vec<T> {17impl<T: Name> Name for Vec<T> {
18 const SIGNATURE: SignatureUnit =
18 evm_coder::make_signature!(new nameof(T) fixed("[]"));19 evm_coder::make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
19}20}
2021
21struct MaxSize();22struct MaxSize();
modifiedcrates/evm-coder/tests/build_failed/custom_signature_over_max_size.stderrdiffbeforeafterboth
1warning: unused import: `make_signature`
2 --> tests/build_failed/custom_signature_over_max_size.rs:5:2
3 |
45 | make_signature,
5 | ^^^^^^^^^^^^^^
6 |
7 = note: `#[warn(unused_imports)]` on by default
8
1error: any use of this value will cause an error9error: any use of this value will cause an error
2 --> tests/build_failed/custom_signature_over_max_size.rs:18:210 --> tests/build_failed/custom_signature_over_max_size.rs:19:3
3 |11 |
418 | evm_coder::make_signature!(new nameof(T) fixed("[]"));1218 | const SIGNATURE: SignatureUnit =
13 | ------------------------------
1419 | evm_coder::make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
5 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 256 but the index is 25615 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 256 but the index is 256
6 |16 |
7 = note: `#[deny(const_err)]` on by default17 = note: `#[deny(const_err)]` on by default
8 = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!18 = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9 = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>19 = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
10 = 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)20 = 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)
1121
12error: any use of this value will cause an error22error: any use of this value will cause an error
13 --> tests/build_failed/custom_signature_over_max_size.rs:29:2923 --> tests/build_failed/custom_signature_over_max_size.rs:30:29
14 |24 |
1529 | const NAME: SignatureUnit = <Vec<MaxSize>>::SIGNATURE;2530 | const NAME: SignatureUnit = <Vec<MaxSize>>::SIGNATURE;
16 | ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors26 | ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
17 |27 |
18 = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!28 = 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
1use evm_coder::{types::*, solidity_interface, execution::Result};1use evm_coder::{types::*, solidity_interface, execution::Result};
2use evm_coder::{
3 make_signature,
4 custom_signature::{
5 SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
6 },
7 types::Signature,
8};
92
10pub struct Contract(bool);3pub struct Contract(bool);
114
modifiedcrates/evm-coder/tests/generics.rsdiffbeforeafterboth
1616
17use std::marker::PhantomData;17use std::marker::PhantomData;
18use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};18use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
19use evm_coder::{
20 make_signature,
21 custom_signature::{
22 SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
23 },
24};
2519
26pub struct Generic<T>(PhantomData<T>);20pub struct Generic<T>(PhantomData<T>);
2721
modifiedcrates/evm-coder/tests/random.rsdiffbeforeafterboth
1818
19use evm_coder::{ToLog, execution::Result, solidity_interface, types::*, solidity, weight};19use evm_coder::{ToLog, execution::Result, solidity_interface, types::*, solidity, weight};
20use evm_coder::{20use evm_coder::{types::Signature};
21 make_signature,
22 custom_signature::{
23 SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
24 },
25 types::Signature,
26};
2721
28pub struct Impls;22pub struct Impls;
modifiedcrates/evm-coder/tests/solidity_generation.rsdiffbeforeafterboth
1616
17use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};17use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
18use evm_coder::{18use evm_coder::{types::Signature};
19 make_signature,
20 custom_signature::{
21 SIGNATURE_SIZE_LIMIT, SignatureUnit, SignaturePreferences, FunctionSignature,
22 },
23 types::Signature,
24};
2519
26pub struct ERC20;20pub struct ERC20;