@@ -40,7 +40,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
40
40
41
41
let functions_enum = if functions. len ( ) > 1 {
42
42
let mut attrs = d_attrs. clone ( ) ;
43
- let doc_str = format ! ( "Container for all the [ `{name}`] function calls." ) ;
43
+ let doc_str = format ! ( "Container for all the `{name}` function calls." ) ;
44
44
attrs. push ( parse_quote ! ( #[ doc = #doc_str] ) ) ;
45
45
Some ( expand_functions_enum ( cx, name, functions, & attrs) )
46
46
} else {
@@ -49,7 +49,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
49
49
50
50
let errors_enum = if errors. len ( ) > 1 {
51
51
let mut attrs = d_attrs;
52
- let doc_str = format ! ( "Container for all the [ `{name}`] custom errors." ) ;
52
+ let doc_str = format ! ( "Container for all the `{name}` custom errors." ) ;
53
53
attrs. push ( parse_quote ! ( #[ doc = #doc_str] ) ) ;
54
54
Some ( expand_errors_enum ( cx, name, errors, & attrs) )
55
55
} else {
@@ -71,11 +71,11 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
71
71
72
72
fn expand_functions_enum (
73
73
cx : & ExpCtxt < ' _ > ,
74
- name : & SolIdent ,
74
+ contract_name : & SolIdent ,
75
75
functions : Vec < & ItemFunction > ,
76
76
attrs : & [ Attribute ] ,
77
77
) -> TokenStream {
78
- let name = format_ident ! ( "{name }Calls" ) ;
78
+ let name = format_ident ! ( "{contract_name }Calls" ) ;
79
79
let variants: Vec < _ > = functions
80
80
. iter ( )
81
81
. map ( |f| cx. function_name_ident ( f) . 0 )
@@ -86,24 +86,24 @@ fn expand_functions_enum(
86
86
let min_data_len = functions
87
87
. iter ( )
88
88
. map ( |function| r#type:: params_min_data_size ( cx, & function. arguments ) )
89
- . max ( )
89
+ . min ( )
90
90
. unwrap ( ) ;
91
91
let trt = Ident :: new ( "SolCall" , Span :: call_site ( ) ) ;
92
92
expand_call_like_enum ( name, & variants, & types, min_data_len, trt, attrs)
93
93
}
94
94
95
95
fn expand_errors_enum (
96
96
cx : & ExpCtxt < ' _ > ,
97
- name : & SolIdent ,
97
+ contract_name : & SolIdent ,
98
98
errors : Vec < & ItemError > ,
99
99
attrs : & [ Attribute ] ,
100
100
) -> TokenStream {
101
- let name = format_ident ! ( "{name }Errors" ) ;
101
+ let name = format_ident ! ( "{contract_name }Errors" ) ;
102
102
let variants: Vec < _ > = errors. iter ( ) . map ( |error| error. name . 0 . clone ( ) ) . collect ( ) ;
103
103
let min_data_len = errors
104
104
. iter ( )
105
105
. map ( |error| r#type:: params_min_data_size ( cx, & error. parameters ) )
106
- . max ( )
106
+ . min ( )
107
107
. unwrap ( ) ;
108
108
let trt = Ident :: new ( "SolError" , Span :: call_site ( ) ) ;
109
109
expand_call_like_enum ( name, & variants, & variants, min_data_len, trt, attrs)
@@ -120,64 +120,75 @@ fn expand_call_like_enum(
120
120
assert_eq ! ( variants. len( ) , types. len( ) ) ;
121
121
let name_s = name. to_string ( ) ;
122
122
let count = variants. len ( ) ;
123
- let min_data_len = min_data_len. min ( 4 ) ;
124
123
quote ! {
125
124
#( #attrs) *
126
125
pub enum #name {
127
126
#( #variants( #types) , ) *
128
127
}
129
128
130
- // TODO: Implement these functions using traits?
131
129
#[ automatically_derived]
132
- impl #name {
133
- /// The number of variants.
134
- pub const COUNT : usize = #count;
135
-
136
- // no decode_raw is possible because we need the selector to know which variant to
137
- // decode into
138
-
139
- /// ABI-decodes the given data into one of the variants of `self`.
140
- pub fn decode( data: & [ u8 ] , validate: bool ) -> :: alloy_sol_types:: Result <Self > {
141
- if data. len( ) >= #min_data_len {
142
- // TODO: Replace with `data.split_array_ref` once it's stable
143
- let ( selector, data) = data. split_at( 4 ) ;
144
- let selector: & [ u8 ; 4 ] =
145
- :: core:: convert:: TryInto :: try_into( selector) . expect( "unreachable" ) ;
146
- match * selector {
147
- #( <#types as :: alloy_sol_types:: #trt>:: SELECTOR => {
148
- return <#types as :: alloy_sol_types:: #trt>:: decode_raw( data, validate)
149
- . map( Self :: #variants)
150
- } ) *
151
- _ => { }
152
- }
130
+ impl :: alloy_sol_types:: SolCalls for #name {
131
+ const NAME : & ' static str = #name_s;
132
+ const MIN_DATA_LENGTH : usize = #min_data_len;
133
+ const COUNT : usize = #count;
134
+
135
+ #[ inline]
136
+ fn selector( & self ) -> [ u8 ; 4 ] {
137
+ match self { #(
138
+ Self :: #variants( _) => <#types as :: alloy_sol_types:: #trt>:: SELECTOR ,
139
+ ) * }
140
+ }
141
+
142
+ #[ inline]
143
+ fn type_check( selector: [ u8 ; 4 ] ) -> :: alloy_sol_types:: Result <( ) > {
144
+ match selector {
145
+ #( <#types as :: alloy_sol_types:: #trt>:: SELECTOR ) |* => Ok ( ( ) ) ,
146
+ s => :: core:: result:: Result :: Err ( :: alloy_sol_types:: Error :: unknown_selector(
147
+ Self :: NAME ,
148
+ s,
149
+ ) ) ,
153
150
}
154
- :: core:: result:: Result :: Err ( :: alloy_sol_types:: Error :: type_check_fail(
155
- data,
156
- #name_s,
157
- ) )
158
151
}
159
152
160
- /// ABI-encodes `self` into the given buffer.
161
- pub fn encode_raw( & self , out: & mut Vec <u8 >) {
153
+ #[ inline]
154
+ fn decode_raw(
155
+ selector: [ u8 ; 4 ] ,
156
+ data: & [ u8 ] ,
157
+ validate: bool
158
+ ) -> :: alloy_sol_types:: Result <Self > {
159
+ match selector {
160
+ #( <#types as :: alloy_sol_types:: #trt>:: SELECTOR => {
161
+ <#types as :: alloy_sol_types:: #trt>:: decode_raw( data, validate)
162
+ . map( Self :: #variants)
163
+ } ) *
164
+ s => :: core:: result:: Result :: Err ( :: alloy_sol_types:: Error :: unknown_selector(
165
+ Self :: NAME ,
166
+ s,
167
+ ) ) ,
168
+ }
169
+ }
170
+
171
+ #[ inline]
172
+ fn encoded_size( & self ) -> usize {
162
173
match self { #(
163
174
Self :: #variants( inner) =>
164
- <#types as :: alloy_sol_types:: #trt>:: encode_raw ( inner, out ) ,
175
+ <#types as :: alloy_sol_types:: #trt>:: encoded_size ( inner) ,
165
176
) * }
166
177
}
167
178
168
- /// ABI-encodes `self` into the given buffer.
169
179
#[ inline]
170
- pub fn encode ( & self ) -> Vec <u8 > {
180
+ fn encode_raw ( & self , out : & mut Vec <u8 >) {
171
181
match self { #(
172
182
Self :: #variants( inner) =>
173
- <#types as :: alloy_sol_types:: #trt>:: encode ( inner) ,
183
+ <#types as :: alloy_sol_types:: #trt>:: encode_raw ( inner, out ) ,
174
184
) * }
175
185
}
176
186
}
177
187
178
188
#(
179
189
#[ automatically_derived]
180
190
impl From <#types> for #name {
191
+ #[ inline]
181
192
fn from( value: #types) -> Self {
182
193
Self :: #variants( value)
183
194
}
0 commit comments