Trait AsArg

pub trait AsArg<T>: Sized
where T: ToGodot,
{ }
Expand description

Implicit conversions for arguments passed to Godot APIs.

An impl AsArg<T> parameter allows values to be passed which can be represented in the target type T. Note that unlike From<T>, this trait is implemented more conservatively.

As a result, AsArg<T> is currently only implemented for certain argument types:

  • T for by-value built-ins (typically Copy): i32, bool, Vector3, Transform2D, …
  • &T for by-ref built-ins: GString, Array, Dictionary, Packed*Array, Variant
  • &str, &String additionally for string types GString, StringName, NodePath.

See also the AsObjectArg trait which is specialized for object arguments. It may be merged with AsArg in the future.

§Pass by value

Implicitly converting from T for by-ref built-ins is explicitly not supported. This emphasizes that there is no need to consume the object, thus discourages unnecessary cloning.

§Performance for strings

Godot has three string types: GString, StringName and NodePath. Conversions between those three, as well as between String and them, is generally expensive because of allocations, re-encoding, validations, hashing, etc. While this doesn’t matter for a few strings passed to engine APIs, it can become a problematic when passing long strings in a hot loop.

In the case of strings, we allow implicit conversion from Rust types &str, &String and &'static CStr (StringName only). While these conversions are not free, those are either explicit because a string literal is used, or they are unsurprising, because Godot cannot deal with raw Rust types. On the other hand, GString and StringName are sometimes used almost interchangeably (example: Node::set_name takes GString but Node::get_name returns StringName).

If you want to convert between Godot’s string types for the sake of argument passing, each type provides an arg() method, such as GString::arg(). You cannot use this method in other contexts.

§Using the trait

AsArg is meant to be used from the function call site, not the declaration site. If you declare a parameter as impl AsArg<...> yourself, you can only forward it as-is to a Godot API – there are no stable APIs to access the inner object yet.

If you want to pass your own types to a Godot API i.e. to emit a signal, you should implement the ParamType trait.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl AsArg<GString> for &str

§

impl AsArg<GString> for &String

§

impl AsArg<NodePath> for &str

§

impl AsArg<NodePath> for &String

§

impl AsArg<StringName> for &'static CStr

Available on since_api="4.2" only.
§

impl AsArg<StringName> for &str

§

impl AsArg<StringName> for &String

§

impl<T> AsArg<Option<Gd<T>>> for Option<&Gd<T>>
where T: GodotClass,

§

impl<T> AsArg<T> for &T
where T: ToGodot + ParamType<ArgPassing = ByRef>,

Implementors§

§

impl<T> AsArg<T> for T
where T: ToGodot + ParamType<ArgPassing = ByValue>,