Skip to content

Commit 56f369f

Browse files
committed
Remove Result from custom callables
1 parent fd16af5 commit 56f369f

File tree

15 files changed

+58
-52
lines changed

15 files changed

+58
-52
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Callable {
111111
&function_name,
112112
args.as_slice(),
113113
);
114-
Ok(result)
114+
result
115115
})
116116
}
117117

@@ -139,14 +139,16 @@ impl Callable {
139139
///
140140
/// 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,
141141
/// 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
143144
where
144-
F: 'static + FnMut(&[&Variant]) -> Result<Variant, ()>,
145+
R: ToGodot,
146+
F: 'static + FnMut(&[&Variant]) -> R,
145147
S: meta::AsArg<GString>,
146148
{
147149
meta::arg_into_owned!(name);
148150

149-
Self::from_fn_wrapper(FnWrapper {
151+
Self::from_fn_wrapper::<F, R>(FnWrapper {
150152
rust_function,
151153
name,
152154
thread_id: Some(std::thread::current().id()),
@@ -165,12 +167,12 @@ impl Callable {
165167
pub fn from_linked_fn<F, T, S>(name: S, linked_object: &Gd<T>, rust_function: F) -> Self
166168
where
167169
T: GodotClass,
168-
F: 'static + FnMut(&[&Variant]) -> Result<Variant, ()>,
170+
F: 'static + FnMut(&[&Variant]) -> Variant,
169171
S: meta::AsArg<GString>,
170172
{
171173
meta::arg_into_owned!(name);
172174

173-
Self::from_fn_wrapper(FnWrapper {
175+
Self::from_fn_wrapper::<F, Variant>(FnWrapper {
174176
rust_function,
175177
name,
176178
thread_id: Some(std::thread::current().id()),
@@ -186,7 +188,7 @@ impl Callable {
186188
/// useful for deferred operations that should only execute once. For repeated execution, use [`from_local_fn()][Self::from_local_fn].
187189
pub(crate) fn from_once_fn<F, S>(name: S, rust_function: F) -> Self
188190
where
189-
F: 'static + FnOnce(&[&Variant]) -> Result<Variant, ()>,
191+
F: 'static + FnOnce(&[&Variant]) -> Variant,
190192
S: meta::AsArg<GString>,
191193
{
192194
meta::arg_into_owned!(name);
@@ -204,7 +206,7 @@ impl Callable {
204206
#[doc(hidden)]
205207
pub fn __once_fn<F, S>(name: S, rust_function: F) -> Self
206208
where
207-
F: 'static + FnOnce(&[&Variant]) -> Result<Variant, ()>,
209+
F: 'static + FnOnce(&[&Variant]) -> Variant,
208210
S: meta::AsArg<GString>,
209211
{
210212
Self::from_once_fn(name, rust_function)
@@ -213,12 +215,12 @@ impl Callable {
213215
pub(crate) fn with_scoped_fn<S, F, Fc, R>(name: S, rust_function: F, callable_usage: Fc) -> R
214216
where
215217
S: meta::AsArg<GString>,
216-
F: FnMut(&[&Variant]) -> Result<Variant, ()>,
218+
F: FnMut(&[&Variant]) -> Variant,
217219
Fc: FnOnce(&Callable) -> R,
218220
{
219221
meta::arg_into_owned!(name);
220222

221-
let callable = Self::from_fn_wrapper(FnWrapper {
223+
let callable = Self::from_fn_wrapper::<F, Variant>(FnWrapper {
222224
rust_function,
223225
name,
224226
thread_id: Some(std::thread::current().id()),
@@ -250,12 +252,12 @@ impl Callable {
250252
#[cfg(feature = "experimental-threads")]
251253
pub fn from_sync_fn<F, S>(name: S, rust_function: F) -> Self
252254
where
253-
F: 'static + Send + Sync + FnMut(&[&Variant]) -> Result<Variant, ()>,
255+
F: 'static + Send + Sync + FnMut(&[&Variant]) -> Variant,
254256
S: meta::AsArg<GString>,
255257
{
256258
meta::arg_into_owned!(name);
257259

258-
Self::from_fn_wrapper(FnWrapper {
260+
Self::from_fn_wrapper::<F, Variant>(FnWrapper {
259261
rust_function,
260262
name,
261263
thread_id: None,
@@ -287,9 +289,10 @@ impl Callable {
287289
Self::from_custom_info(info)
288290
}
289291

290-
fn from_fn_wrapper<F>(inner: FnWrapper<F>) -> Self
292+
fn from_fn_wrapper<F, R>(inner: FnWrapper<F>) -> Self
291293
where
292-
F: FnMut(&[&Variant]) -> Result<Variant, ()>,
294+
F: FnMut(&[&Variant]) -> R,
295+
R: ToGodot,
293296
{
294297
let object_id = inner.linked_object_id();
295298

@@ -298,7 +301,7 @@ impl Callable {
298301
let info = CallableCustomInfo {
299302
object_id,
300303
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>),
302305
free_func: Some(rust_callable_destroy::<FnWrapper<F>>),
303306
to_string_func: Some(rust_callable_to_string_named::<F>),
304307
is_valid_func: Some(rust_callable_is_valid),
@@ -593,7 +596,7 @@ mod custom_callable {
593596
/// Return `Ok(...)` if the call succeeded, and `Err(())` otherwise.
594597
/// Error handling is mostly needed in case argument number or types mismatch.
595598
#[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;
597600

598601
// TODO(v0.3): add object_id().
599602

@@ -626,19 +629,20 @@ mod custom_callable {
626629
// Get the RustCallable again inside closure so it doesn't have to be UnwindSafe.
627630
let c: &mut C = CallableUserdata::inner_from_raw(callable_userdata);
628631
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);
630633
Ok(())
631634
});
632635
}
633636

634-
pub unsafe extern "C" fn rust_callable_call_fn<F>(
637+
pub unsafe extern "C" fn rust_callable_call_fn<F, R>(
635638
callable_userdata: *mut std::ffi::c_void,
636639
p_args: *const sys::GDExtensionConstVariantPtr,
637640
p_argument_count: sys::GDExtensionInt,
638641
r_return: sys::GDExtensionVariantPtr,
639642
r_error: *mut sys::GDExtensionCallError,
640643
) where
641-
F: FnMut(&[&Variant]) -> Result<Variant, ()>,
644+
F: FnMut(&[&Variant]) -> R,
645+
R: ToGodot,
642646
{
643647
let arg_refs: &[&Variant] = Variant::borrow_ref_slice(p_args, p_argument_count as usize);
644648

@@ -664,8 +668,8 @@ mod custom_callable {
664668
);
665669
}
666670

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);
669673
Ok(())
670674
});
671675
}

godot-core/src/builtin/collections/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl<T: ArrayElement> Array<T> {
710710
let value = T::from_variant(args[0]);
711711
let is_less = matches!(func(&value), cmp::Ordering::Less);
712712

713-
Ok(is_less.to_variant())
713+
is_less.to_variant()
714714
};
715715

716716
let debug_name = std::any::type_name::<F>();
@@ -787,7 +787,7 @@ impl<T: ArrayElement> Array<T> {
787787
let rhs = T::from_variant(args[1]);
788788
let is_less = matches!(func(&lhs, &rhs), cmp::Ordering::Less);
789789

790-
Ok(is_less.to_variant())
790+
is_less.to_variant()
791791
};
792792

793793
let debug_name = std::any::type_name::<F>();

godot-core/src/obj/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<T: GodotClass> Base<T> {
226226
let name = format!("Base<{}> deferred unref", T::class_id());
227227
let callable = Callable::from_once_fn(&name, move |_args| {
228228
Self::drop_strong_ref(instance_id);
229-
Ok(Variant::nil())
229+
Variant::nil()
230230
});
231231

232232
// Use Callable::call_deferred() instead of Gd::apply_deferred(). The latter implicitly borrows &mut self,

godot-core/src/obj/gd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ impl<T: GodotClass> Gd<T> {
586586
/// If you need a Callable which can live indefinitely use [`Callable::from_local_fn()`].
587587
pub fn linked_callable<F>(&self, method_name: impl AsArg<GString>, rust_function: F) -> Callable
588588
where
589-
F: 'static + FnMut(&[&Variant]) -> Result<Variant, ()>,
589+
F: 'static + FnMut(&[&Variant]) -> Variant,
590590
{
591591
Callable::from_linked_fn(method_name, self, rust_function)
592592
}
@@ -711,7 +711,7 @@ impl<T: GodotClass> Gd<T> {
711711

712712
let callable = Callable::from_once_fn("run_deferred", move |_| {
713713
gd_function(obj);
714-
Ok(Variant::nil())
714+
Variant::nil()
715715
});
716716
callable.call_deferred(&[]);
717717
}

godot-core/src/registry/signal/connect_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ where
126126
/// type state builder for simple + common connections, thus hopefully being a tiny bit lighter on compile times.
127127
fn inner_connect_godot_fn<F>(
128128
self,
129-
godot_fn: impl FnMut(&[&Variant]) -> Result<Variant, ()> + 'static,
129+
godot_fn: impl FnMut(&[&Variant]) -> Variant + 'static,
130130
bound: &Gd<impl GodotClass>,
131131
) -> ConnectHandle {
132132
let callable_name = match &self.data.callable_name {

godot-core/src/registry/signal/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ pub mod priv_re_export {
3939
// ----------------------------------------------------------------------------------------------------------------------------------------------
4040

4141
// Used by both `TypedSignal` and `ConnectBuilder`.
42-
fn make_godot_fn<Ps, F>(mut input: F) -> impl FnMut(&[&Variant]) -> Result<Variant, ()>
42+
fn make_godot_fn<Ps, F>(mut input: F) -> impl FnMut(&[&Variant]) -> Variant
4343
where
4444
F: FnMut(Ps),
4545
Ps: meta::InParamTuple,
4646
{
47-
move |variant_args: &[&Variant]| -> Result<Variant, ()> {
47+
move |variant_args: &[&Variant]| {
4848
let args = Ps::from_variant_array(variant_args);
4949
input(args);
50-
51-
Ok(Variant::nil())
50+
Variant::nil()
5251
}
5352
}
5453

godot-core/src/registry/signal/typed_signal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'c, C: WithSignals, Ps: meta::ParamTuple> TypedSignal<'c, C, Ps> {
149149
/// type state builder for simple + common connections, thus hopefully being a tiny bit lighter on compile times.
150150
fn inner_connect_godot_fn<F>(
151151
&self,
152-
godot_fn: impl FnMut(&[&Variant]) -> Result<Variant, ()> + 'static,
152+
godot_fn: impl FnMut(&[&Variant]) -> Variant + 'static,
153153
bound: &Gd<impl GodotClass>,
154154
) -> ConnectHandle {
155155
let callable_name = make_callable_name::<F>();

godot-core/src/task/async_runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl Wake for GodotWaker {
500500
/// This appears to be a common issue: https://github.com/rust-lang/rust/issues/89976
501501
fn callback_type_hint<F>(f: F) -> F
502502
where
503-
F: for<'a> FnMut(&'a [&Variant]) -> Result<Variant, ()>,
503+
F: for<'a> FnMut(&'a [&Variant]) -> Variant,
504504
{
505505
f
506506
}
@@ -515,7 +515,7 @@ impl Wake for GodotWaker {
515515
"GodotWaker::wake",
516516
callback_type_hint(move |_args| {
517517
poll_future(waker.take().expect("Callable will never be called again"));
518-
Ok(Variant::nil())
518+
Variant::nil()
519519
}),
520520
);
521521

godot-core/src/task/futures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<R: IntoDynamicSend> PartialEq for SignalFutureResolver<R> {
115115
}
116116

117117
impl<R: InParamTuple + IntoDynamicSend> RustCallable for SignalFutureResolver<R> {
118-
fn invoke(&mut self, args: &[&Variant]) -> Result<Variant, ()> {
118+
fn invoke(&mut self, args: &[&Variant]) -> Variant {
119119
let waker = {
120120
let mut data = self.data.lock().unwrap();
121121
data.state = SignalFutureState::Ready(R::from_variant_array(args).into_dynamic_send());
@@ -128,7 +128,7 @@ impl<R: InParamTuple + IntoDynamicSend> RustCallable for SignalFutureResolver<R>
128128
waker.wake();
129129
}
130130

131-
Ok(Variant::nil())
131+
Variant::nil()
132132
}
133133
}
134134

itest/rust/src/builtin_tests/containers/array_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ fn array_bsearch_custom() {
564564
fn backwards_sort_callable() -> Callable {
565565
Callable::from_local_fn("sort backwards", |args: &[&Variant]| {
566566
let res = args[0].to::<i32>() > args[1].to::<i32>();
567-
Ok(res.to_variant())
567+
res.to_variant()
568568
})
569569
}
570570

0 commit comments

Comments
 (0)