@@ -131,39 +131,41 @@ impl DynamicItems {
131
131
assert_eq ! ( args. len( ) , args_identifiers. len( ) ) ;
132
132
}
133
133
134
- self . struct_members . push (
135
- if is_required {
136
- quote ! {
137
- pub #ident: unsafe extern #abi fn ( #( #args) , * ) #ret,
138
- }
139
- } else {
140
- quote ! {
141
- pub #ident: Result <unsafe extern #abi fn ( #( #args ) , * ) #ret, :: libloading:: Error >,
142
- }
134
+ let signature = quote ! { unsafe extern #abi fn ( #( #args) , * ) #ret } ;
135
+ let member = if is_required {
136
+ signature
137
+ } else {
138
+ quote ! { Result <#signature, :: libloading:: Error > }
139
+ } ;
140
+
141
+ self . struct_members . push ( quote ! {
142
+ pub #ident: #member,
143
+ } ) ;
144
+
145
+ // N.B: If the signature was required, it won't be wrapped in a Result<...>
146
+ // and we can simply call it directly.
147
+ let call_body = if is_required {
148
+ quote ! {
149
+ self . #ident( #( #args_identifiers ) , * )
150
+ }
151
+ } else {
152
+ quote ! {
153
+ let sym = self . #ident. as_ref( ) . expect( "Expected function, got error." ) ;
154
+ ( sym) ( #( #args_identifiers ) , * )
143
155
}
144
- ) ;
156
+ } ;
145
157
146
158
// We can't implement variadic functions from C easily, so we allow to
147
159
// access the function pointer so that the user can call it just fine.
148
160
if !is_variadic {
149
- self . struct_implementation . push (
150
- if is_required {
151
- quote ! {
152
- pub unsafe fn #ident ( & self , #( #args ) , * ) -> #ret_ty {
153
- self . #ident( #( #args_identifiers ) , * )
154
- }
155
- }
156
- } else {
157
- quote ! {
158
- pub unsafe fn #ident ( & self , #( #args ) , * ) -> #ret_ty {
159
- let sym = self . #ident. as_ref( ) . expect( "Expected function, got error." ) ;
160
- ( sym) ( #( #args_identifiers ) , * )
161
- }
162
- }
161
+ self . struct_implementation . push ( quote ! {
162
+ pub unsafe fn #ident ( & self , #( #args ) , * ) -> #ret_ty {
163
+ #call_body
163
164
}
164
- ) ;
165
+ } ) ;
165
166
}
166
167
168
+ // N.B: Unwrap the signature upon construction if it is required to be resolved.
167
169
let ident_str = codegen:: helpers:: ast_ty:: cstr_expr ( ident. to_string ( ) ) ;
168
170
self . constructor_inits . push ( if is_required {
169
171
quote ! {
0 commit comments