godot::meta

Trait AsObjectArg

pub trait AsObjectArg<T>
where T: GodotClass<Declarer = DeclEngine> + Bounds,
{ }
Expand description

Objects that can be passed as arguments to Godot engine functions.

This trait is implemented for shared references in multiple ways:

  • &Gd<T> to pass objects. Subclasses of T are explicitly supported.
  • Option<&Gd<T>>, to pass optional objects. None is mapped to a null argument.
  • Gd::null_arg(), to pass null arguments without using Option.

Note that AsObjectArg is very similar to the more general AsArg trait. The two may be merged in the future.

§Nullability

The GDExtension API does not inform about nullability of its function parameters. It is up to you to verify that the arguments you pass are only null when this is allowed. Doing this wrong should be safe, but can lead to the function call failing.

§Different argument types

Currently, the trait requires pass-by-ref, which helps detect accidental cloning when interfacing with Godot APIs. Plus, it is more consistent with the AsArg trait (for strings, but also AsArg<Gd<T>> as used in Array::push() and similar methods).

The following table lists the possible argument types and how you can pass them. Gd is short for Gd<T>.

TypeClosest accepted typeHow to transform
Gd&Gd&arg
&Gd&Gdarg
&mut Gd&Gd&*arg
Option<Gd>Option<&Gd>arg.as_ref()
Option<&Gd>Option<&Gd>arg
Option<&mut Gd>Option<&Gd>arg.as_deref()
(null literal)Gd::null_arg()

Implementations on Foreign Types§

§

impl<T, U> AsObjectArg<T> for Option<U>
where T: GodotClass<Declarer = DeclEngine> + Bounds, U: AsObjectArg<T>,

Implementors§

§

impl<T, U> AsObjectArg<T> for &Gd<U>
where T: GodotClass<Declarer = DeclEngine> + Bounds, U: Inherits<T>,

§

impl<T, U, D> AsObjectArg<T> for &DynGd<U, D>
where T: GodotClass<Declarer = DeclEngine> + Bounds, U: Inherits<T>, D: ?Sized,