@@ -9,6 +9,7 @@ use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
9
9
use std:: ops:: { Deref , DerefMut } ;
10
10
11
11
use godot_ffi as sys;
12
+ use godot_ffi:: is_main_thread;
12
13
use sys:: { static_assert_eq_size_align, SysPtr as _} ;
13
14
14
15
use crate :: builtin:: { Callable , NodePath , StringName , Variant } ;
@@ -444,6 +445,25 @@ impl<T: GodotClass> Gd<T> {
444
445
} )
445
446
}
446
447
448
+ /// Runs the given Closure deferred.
449
+ ///
450
+ /// This can be a type-safe alternative to [`classes::Object::call_deferred`], but does not handle dynamic dispatch, unless explicitly used.
451
+ /// This must be used on the main thread.
452
+ #[ cfg( since_api = "4.2" ) ]
453
+ pub fn apply_deferred < F > ( & mut self , mut rust_function : F )
454
+ where
455
+ F : FnMut ( & mut T ) + ' static ,
456
+ T : GodotClass + Bounds < Declarer = bounds:: DeclUser > ,
457
+ {
458
+ assert ! ( !is_main_thread( ) , "apply_deferred must be called on main thread. Consider bind_deferred instead" ) ;
459
+ let this = self . clone ( ) ;
460
+ let callable = Callable :: from_local_fn ( "apply_deferred" , move |_| {
461
+ rust_function ( this. clone ( ) . bind_mut ( ) . deref_mut ( ) ) ;
462
+ Ok ( Variant :: nil ( ) )
463
+ } ) ;
464
+ callable. call_deferred ( & [ ] ) ;
465
+ }
466
+
447
467
/// Returns `Ok(cast_obj)` on success, `Err(self)` on error.
448
468
// Visibility: used by DynGd.
449
469
pub ( crate ) fn owned_cast < U > ( self ) -> Result < Gd < U > , Self >
0 commit comments