@@ -31,8 +31,8 @@ use crate::{classes, out};
3131/// This smart pointer can only hold _objects_ in the Godot sense: instances of Godot classes (`Node`, `RefCounted`, etc.)
3232/// or user-declared structs (declared with `#[derive(GodotClass)]`). It does **not** hold built-in types (`Vector3`, `Color`, `i32`).
3333///
34- /// `Gd<T>` never holds null objects. If you need nullability, use `Option<Gd<T>>`. To pass null objects to engine APIs, use
35- /// [`NullArg`][crate::obj::NullArg] .
34+ /// `Gd<T>` never holds null objects. If you need nullability, use `Option<Gd<T>>`. To pass null objects to engine APIs, you can
35+ /// additionally use [`Gd::null_arg()`] as a shorthand .
3636///
3737/// # Memory management
3838///
@@ -466,26 +466,7 @@ impl<T: GodotClass> Gd<T> {
466466 {
467467 self . raw . script_sys ( )
468468 }
469- }
470-
471- impl < T : GodotClass > Deref for Gd < T > {
472- // Target is always an engine class:
473- // * if T is an engine class => T
474- // * if T is a user class => T::Base
475- type Target = GdDerefTarget < T > ;
476469
477- fn deref ( & self ) -> & Self :: Target {
478- self . raw . as_target ( )
479- }
480- }
481-
482- impl < T : GodotClass > DerefMut for Gd < T > {
483- fn deref_mut ( & mut self ) -> & mut Self :: Target {
484- self . raw . as_target_mut ( )
485- }
486- }
487-
488- impl < T : GodotClass > Gd < T > {
489470 /// Runs `init_fn` on the address of a pointer (initialized to null). If that pointer is still null after the `init_fn` call,
490471 /// then `None` will be returned; otherwise `Gd::from_obj_sys(ptr)`.
491472 ///
@@ -635,9 +616,56 @@ where
635616 }
636617}
637618
619+ impl < T > Gd < T >
620+ where
621+ T : GodotClass + Bounds < Declarer = bounds:: DeclEngine > ,
622+ {
623+ /// Represents `null` when passing an object argument to Godot.
624+ ///
625+ /// This expression is only intended for function argument lists. It can be used whenever a Godot signature accepts
626+ /// [`AsObjectArg<T>`][crate::obj::AsObjectArg]. `Gd::null_arg()` as an argument is equivalent to `Option::<Gd<T>>::None`, but less wordy.
627+ ///
628+ /// To work with objects that can be null, use `Option<Gd<T>>` instead. For APIs that accept `Variant`, you can pass [`Variant::nil()`].
629+ ///
630+ /// # Nullability
631+ /// <div class="warning">
632+ /// The GDExtension API does not inform about nullability of its function parameters. It is up to you to verify that the arguments you pass
633+ /// are only null when this is allowed. Doing this wrong should be safe, but can lead to the function call failing.
634+ /// </div>
635+ ///
636+ /// # Example
637+ /// ```no_run
638+ /// # fn some_shape() -> Gd<GltfPhysicsShape> { unimplemented!() }
639+ /// use godot::prelude::*;
640+ /// use godot::classes::GltfPhysicsShape;
641+ ///
642+ /// let mut shape: Gd<GltfPhysicsShape> = some_shape();
643+ /// shape.set_importer_mesh(Gd::null_arg());
644+ pub fn null_arg ( ) -> crate :: obj:: ObjectNullArg < T > {
645+ crate :: obj:: ObjectNullArg ( std:: marker:: PhantomData )
646+ }
647+ }
648+
638649// ----------------------------------------------------------------------------------------------------------------------------------------------
639650// Trait impls
640651
652+ impl < T : GodotClass > Deref for Gd < T > {
653+ // Target is always an engine class:
654+ // * if T is an engine class => T
655+ // * if T is a user class => T::Base
656+ type Target = GdDerefTarget < T > ;
657+
658+ fn deref ( & self ) -> & Self :: Target {
659+ self . raw . as_target ( )
660+ }
661+ }
662+
663+ impl < T : GodotClass > DerefMut for Gd < T > {
664+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
665+ self . raw . as_target_mut ( )
666+ }
667+ }
668+
641669impl < T : GodotClass > GodotConvert for Gd < T > {
642670 type Via = Gd < T > ;
643671}
0 commit comments