@@ -111,7 +111,7 @@ impl Callable {
111
111
& function_name,
112
112
args. as_slice ( ) ,
113
113
) ;
114
- Ok ( result)
114
+ result
115
115
} )
116
116
}
117
117
@@ -139,14 +139,16 @@ impl Callable {
139
139
///
140
140
/// This constructor only allows the callable to be invoked from the same thread as creating it. If you need to invoke it from any thread,
141
141
/// use [`from_sync_fn`][Self::from_sync_fn] instead (requires crate feature `experimental-threads`; only enable if really needed).
142
- pub fn from_local_fn < F , S > ( name : S , rust_function : F ) -> Self
142
+ #[ cfg( since_api = "4.2" ) ]
143
+ pub fn from_local_fn < R , F , S > ( name : S , rust_function : F ) -> Self
143
144
where
144
- F : ' static + FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
145
+ R : ToGodot ,
146
+ F : ' static + FnMut ( & [ & Variant ] ) -> R ,
145
147
S : meta:: AsArg < GString > ,
146
148
{
147
149
meta:: arg_into_owned!( name) ;
148
150
149
- Self :: from_fn_wrapper ( FnWrapper {
151
+ Self :: from_fn_wrapper :: < F , R > ( FnWrapper {
150
152
rust_function,
151
153
name,
152
154
thread_id : Some ( std:: thread:: current ( ) . id ( ) ) ,
@@ -165,12 +167,12 @@ impl Callable {
165
167
pub fn from_linked_fn < F , T , S > ( name : S , linked_object : & Gd < T > , rust_function : F ) -> Self
166
168
where
167
169
T : GodotClass ,
168
- F : ' static + FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
170
+ F : ' static + FnMut ( & [ & Variant ] ) -> Variant ,
169
171
S : meta:: AsArg < GString > ,
170
172
{
171
173
meta:: arg_into_owned!( name) ;
172
174
173
- Self :: from_fn_wrapper ( FnWrapper {
175
+ Self :: from_fn_wrapper :: < F , Variant > ( FnWrapper {
174
176
rust_function,
175
177
name,
176
178
thread_id : Some ( std:: thread:: current ( ) . id ( ) ) ,
@@ -186,7 +188,7 @@ impl Callable {
186
188
/// useful for deferred operations that should only execute once. For repeated execution, use [`from_local_fn()][Self::from_local_fn].
187
189
pub ( crate ) fn from_once_fn < F , S > ( name : S , rust_function : F ) -> Self
188
190
where
189
- F : ' static + FnOnce ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
191
+ F : ' static + FnOnce ( & [ & Variant ] ) -> Variant ,
190
192
S : meta:: AsArg < GString > ,
191
193
{
192
194
meta:: arg_into_owned!( name) ;
@@ -204,7 +206,7 @@ impl Callable {
204
206
#[ doc( hidden) ]
205
207
pub fn __once_fn < F , S > ( name : S , rust_function : F ) -> Self
206
208
where
207
- F : ' static + FnOnce ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
209
+ F : ' static + FnOnce ( & [ & Variant ] ) -> Variant ,
208
210
S : meta:: AsArg < GString > ,
209
211
{
210
212
Self :: from_once_fn ( name, rust_function)
@@ -213,12 +215,12 @@ impl Callable {
213
215
pub ( crate ) fn with_scoped_fn < S , F , Fc , R > ( name : S , rust_function : F , callable_usage : Fc ) -> R
214
216
where
215
217
S : meta:: AsArg < GString > ,
216
- F : FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
218
+ F : FnMut ( & [ & Variant ] ) -> Variant ,
217
219
Fc : FnOnce ( & Callable ) -> R ,
218
220
{
219
221
meta:: arg_into_owned!( name) ;
220
222
221
- let callable = Self :: from_fn_wrapper ( FnWrapper {
223
+ let callable = Self :: from_fn_wrapper :: < F , Variant > ( FnWrapper {
222
224
rust_function,
223
225
name,
224
226
thread_id : Some ( std:: thread:: current ( ) . id ( ) ) ,
@@ -250,12 +252,12 @@ impl Callable {
250
252
#[ cfg( feature = "experimental-threads" ) ]
251
253
pub fn from_sync_fn < F , S > ( name : S , rust_function : F ) -> Self
252
254
where
253
- F : ' static + Send + Sync + FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
255
+ F : ' static + Send + Sync + FnMut ( & [ & Variant ] ) -> Variant ,
254
256
S : meta:: AsArg < GString > ,
255
257
{
256
258
meta:: arg_into_owned!( name) ;
257
259
258
- Self :: from_fn_wrapper ( FnWrapper {
260
+ Self :: from_fn_wrapper :: < F , Variant > ( FnWrapper {
259
261
rust_function,
260
262
name,
261
263
thread_id : None ,
@@ -287,9 +289,10 @@ impl Callable {
287
289
Self :: from_custom_info ( info)
288
290
}
289
291
290
- fn from_fn_wrapper < F > ( inner : FnWrapper < F > ) -> Self
292
+ fn from_fn_wrapper < F , R > ( inner : FnWrapper < F > ) -> Self
291
293
where
292
- F : FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
294
+ F : FnMut ( & [ & Variant ] ) -> R ,
295
+ R : ToGodot ,
293
296
{
294
297
let object_id = inner. linked_object_id ( ) ;
295
298
@@ -298,7 +301,7 @@ impl Callable {
298
301
let info = CallableCustomInfo {
299
302
object_id,
300
303
callable_userdata : Box :: into_raw ( Box :: new ( userdata) ) as * mut std:: ffi:: c_void ,
301
- call_func : Some ( rust_callable_call_fn :: < F > ) ,
304
+ call_func : Some ( rust_callable_call_fn :: < F , R > ) ,
302
305
free_func : Some ( rust_callable_destroy :: < FnWrapper < F > > ) ,
303
306
to_string_func : Some ( rust_callable_to_string_named :: < F > ) ,
304
307
is_valid_func : Some ( rust_callable_is_valid) ,
@@ -593,7 +596,7 @@ mod custom_callable {
593
596
/// Return `Ok(...)` if the call succeeded, and `Err(())` otherwise.
594
597
/// Error handling is mostly needed in case argument number or types mismatch.
595
598
#[ allow( clippy:: result_unit_err) ] // TODO remove once there's a clear error type here.
596
- fn invoke ( & mut self , args : & [ & Variant ] ) -> Result < Variant , ( ) > ;
599
+ fn invoke ( & mut self , args : & [ & Variant ] ) -> Variant ;
597
600
598
601
// TODO(v0.3): add object_id().
599
602
@@ -626,19 +629,20 @@ mod custom_callable {
626
629
// Get the RustCallable again inside closure so it doesn't have to be UnwindSafe.
627
630
let c: & mut C = CallableUserdata :: inner_from_raw ( callable_userdata) ;
628
631
let result = c. invoke ( arg_refs) ;
629
- meta:: varcall_return_checked ( result, r_return, r_error) ;
632
+ meta:: varcall_return_checked ( Ok ( result) , r_return, r_error) ;
630
633
Ok ( ( ) )
631
634
} ) ;
632
635
}
633
636
634
- pub unsafe extern "C" fn rust_callable_call_fn < F > (
637
+ pub unsafe extern "C" fn rust_callable_call_fn < F , R > (
635
638
callable_userdata : * mut std:: ffi:: c_void ,
636
639
p_args : * const sys:: GDExtensionConstVariantPtr ,
637
640
p_argument_count : sys:: GDExtensionInt ,
638
641
r_return : sys:: GDExtensionVariantPtr ,
639
642
r_error : * mut sys:: GDExtensionCallError ,
640
643
) where
641
- F : FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
644
+ F : FnMut ( & [ & Variant ] ) -> R ,
645
+ R : ToGodot ,
642
646
{
643
647
let arg_refs: & [ & Variant ] = Variant :: borrow_ref_slice ( p_args, p_argument_count as usize ) ;
644
648
@@ -664,8 +668,8 @@ mod custom_callable {
664
668
) ;
665
669
}
666
670
667
- let result = ( w. rust_function ) ( arg_refs) ;
668
- meta:: varcall_return_checked ( result, r_return, r_error) ;
671
+ let result = ( w. rust_function ) ( arg_refs) . to_variant ( ) ;
672
+ meta:: varcall_return_checked ( Ok ( result) , r_return, r_error) ;
669
673
Ok ( ( ) )
670
674
} ) ;
671
675
}
0 commit comments