Skip to content

Commit b7f193e

Browse files
committed
Remove WithDeferredCall trait, move to WithBaseField
1 parent ade4666 commit b7f193e

File tree

5 files changed

+42
-109
lines changed

5 files changed

+42
-109
lines changed

godot-core/src/obj/call_deferred.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.

godot-core/src/obj/gd.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,14 @@ impl<T: GodotClass> Gd<T> {
669669

670670
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
671671
///
672-
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred].
672+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
673+
/// `&mut Self` allowing direct access to Rust fields and methods.
673674
///
674-
/// The closure receives `&mut T` allowing direct access to Rust fields and methods.
675675
/// This method is only available for user-defined classes with a `Base<T>` field.
676-
///
677676
/// For engine classes, use [`run_deferred_gd()`][Self::run_deferred_gd] instead.
678677
///
678+
/// See also [`WithBaseField::run_deferred()`] if you are within an `impl` block and have access to `self`.
679+
///
679680
/// # Panics
680681
/// If called outside the main thread.
681682
pub fn run_deferred<F>(&mut self, mut_self_method: F)
@@ -691,10 +692,10 @@ impl<T: GodotClass> Gd<T> {
691692

692693
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
693694
///
694-
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred].
695+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
696+
/// `Gd<T>`, which can be used to call engine methods or [`bind()`][Gd::bind]/[`bind_mut()`][Gd::bind_mut] to access the Rust object.
695697
///
696-
/// The closure receives a `Gd<T>` which can be used to call engine methods.
697-
/// This method works for any Godot class (engine or user-defined).
698+
/// See also [`WithBaseField::run_deferred_gd()`] if you are within an `impl` block and have access to `self`.
698699
///
699700
/// # Panics
700701
/// If called outside the main thread.

godot-core/src/obj/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! * [`Gd`], a smart pointer that manages instances of Godot classes.
1313
1414
mod base;
15-
mod call_deferred;
1615
mod casts;
1716
mod dyn_gd;
1817
mod gd;
@@ -27,7 +26,6 @@ mod traits;
2726
pub(crate) mod rtti;
2827

2928
pub use base::*;
30-
pub use call_deferred::WithDeferredCall;
3129
pub use dyn_gd::DynGd;
3230
pub use gd::*;
3331
pub use guards::{BaseMut, BaseRef, DynGdMut, DynGdRef, GdMut, GdRef};

godot-core/src/obj/traits.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,40 @@ pub trait WithBaseField: GodotClass + Bounds<Declarer = bounds::DeclUser> {
540540
// Narrows lifetime again from 'static to 'self.
541541
BaseMut::new(passive_gd, guard)
542542
}
543+
544+
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
545+
///
546+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
547+
/// `&mut Self` allowing direct access to Rust fields and methods.
548+
///
549+
/// See also [`Gd::run_deferred()`] to defer logic outside of `self`.
550+
///
551+
/// # Panics
552+
/// If called outside the main thread.
553+
fn run_deferred<F>(&mut self, mut_self_method: F)
554+
where
555+
F: FnOnce(&mut Self) + 'static,
556+
{
557+
// We need to copy the Gd, because the lifetime of `&mut self` does not extend throughout the closure, which will only be called
558+
// deferred. It might even be freed in-between, causing panic on bind_mut().
559+
self.to_gd().run_deferred(mut_self_method)
560+
}
561+
562+
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
563+
///
564+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
565+
/// `Gd<Self>`, which can be used to call engine methods or [`bind()`][Gd::bind]/[`bind_mut()`][Gd::bind_mut] to access the Rust object.
566+
///
567+
/// See also [`Gd::run_deferred_gd()`] to defer logic outside of `self`.
568+
///
569+
/// # Panics
570+
/// If called outside the main thread.
571+
fn run_deferred_gd<F>(&mut self, gd_function: F)
572+
where
573+
F: FnOnce(Gd<Self>) + 'static,
574+
{
575+
self.to_gd().run_deferred_gd(gd_function)
576+
}
543577
}
544578

545579
/// Implemented for all classes with registered signals, both engine- and user-declared.

godot/src/prelude.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ mod trait_reexports {
3333
pub use crate::obj::EngineEnum as _;
3434
pub use crate::obj::NewAlloc as _;
3535
pub use crate::obj::NewGd as _;
36-
pub use crate::obj::WithBaseField as _; // base(), base_mut(), to_gd()
37-
pub use crate::obj::WithDeferredCall as _; // apply_deferred()
36+
pub use crate::obj::WithBaseField as _; // base(), base_mut(), to_gd(), run_deferred(), run_deferred_gd()
3837
pub use crate::obj::WithSignals as _; // Gd::signals()
3938
pub use crate::obj::WithUserSignals as _; // self.signals()
4039
}

0 commit comments

Comments
 (0)