godot::meta

Trait AsArg

pub trait AsArg<T>: Sized
where T: ApiParam,
{ }
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 builtins (typically Copy): i32, bool, Vector3, Transform2D, …
  • &T for by-ref builtins: GString, Array, Dictionary, Packed*Array, Variant
  • &str, &String additionally for string types GString, StringName, NodePath.

§Pass by value

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

If you need to pass owned values in generic code, you can use ApiParam::owned_to_arg().

§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.

Furthermore, there is currently no benefit in implementing AsArg for your own types, as it’s only used by Godot APIs which don’t accept custom types. Classes are already supported through upcasting and AsObjectArg.

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<bool> for bool

§

impl AsArg<f32> for f32

§

impl AsArg<f64> for f64

§

impl AsArg<i8> for i8

§

impl AsArg<i16> for i16

§

impl AsArg<i32> for i32

§

impl AsArg<i64> for i64

§

impl AsArg<u8> for u8

§

impl AsArg<u16> for u16

§

impl AsArg<u32> for u32

§

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<'r, T> AsArg<Option<Gd<T>>> for Option<&'r Gd<T>>
where T: GodotClass,

Implementors§

§

impl AsArg<Rid> for Rid

§

impl AsArg<Aabb> for Aabb

§

impl AsArg<Basis> for Basis

§

impl AsArg<Color> for Color

§

impl AsArg<Plane> for Plane

§

impl AsArg<Projection> for Projection

§

impl AsArg<Quaternion> for Quaternion

§

impl AsArg<Rect2> for Rect2

§

impl AsArg<Rect2i> for Rect2i

§

impl AsArg<Transform2D> for Transform2D

§

impl AsArg<Transform3D> for Transform3D

§

impl AsArg<Vector2> for Vector2

§

impl AsArg<Vector2i> for Vector2i

§

impl AsArg<Vector3> for Vector3

§

impl AsArg<Vector3i> for Vector3i

§

impl AsArg<Vector4> for Vector4

§

impl AsArg<Vector4i> for Vector4i

§

impl<'r> AsArg<Callable> for &'r Callable

§

impl<'r> AsArg<Dictionary> for &'r Dictionary

§

impl<'r> AsArg<GString> for &'r GString

§

impl<'r> AsArg<NodePath> for &'r NodePath

§

impl<'r> AsArg<PackedByteArray> for &'r PackedByteArray

§

impl<'r> AsArg<PackedColorArray> for &'r PackedColorArray

§

impl<'r> AsArg<PackedFloat32Array> for &'r PackedFloat32Array

§

impl<'r> AsArg<PackedFloat64Array> for &'r PackedFloat64Array

§

impl<'r> AsArg<PackedInt32Array> for &'r PackedInt32Array

§

impl<'r> AsArg<PackedInt64Array> for &'r PackedInt64Array

§

impl<'r> AsArg<PackedStringArray> for &'r PackedStringArray

§

impl<'r> AsArg<PackedVector2Array> for &'r PackedVector2Array

§

impl<'r> AsArg<PackedVector3Array> for &'r PackedVector3Array

§

impl<'r> AsArg<PackedVector4Array> for &'r PackedVector4Array

§

impl<'r> AsArg<Signal> for &'r Signal

§

impl<'r> AsArg<StringName> for &'r StringName

§

impl<'r> AsArg<Variant> for &'r Variant

§

impl<'r, T> AsArg<Array<T>> for &'r Array<T>
where T: ArrayElement,

§

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