Skip to main content

Element

Trait Element 

pub trait Element:
    ToGodot
    + FromGodot
    + Sealed
    + 'static { }
Expand description

Marker trait to identify types that can be stored in Array<T>.

The types, for which this trait is implemented, overlap mostly with GodotType.

Notable differences are:

  • Only VarArray, not Array<T> is allowed (typed arrays cannot be nested).
  • Option is only supported for Option<Gd<T>>, but not e.g. Option<i32>.

§Integer and float types

u8, i8, u16, i16, u32, i32 and f32 are supported by this trait, however they don’t have their own array type in Godot. The engine only knows about i64 (“int”) and f64 (“float”) types. This means that when using any integer or float type, Godot will treat it as the equivalent of GDScript’s Array[int] or Array[float], respectively.

As a result, when converting from a Godot typed array to a Rust Array<T>, the values stored may not actually fit into a T. For example, you have a GDScript Array[int] which stores value 160, and you convert it to a Rust Array<i8>. This means that you may end up with panics on element access (since the Variant storing 160 will fail to convert to i8). In Debug mode, we add additional best-effort checks to detect such errors, however they are expensive and not bullet-proof. If you need very rigid type safety, stick to i64 and f64. The other types however can be extremely convenient and work well, as long as you are aware of the limitations.

u64 is entirely unsupported.

Also, keep in mind that Godot uses Variant for each element. If performance matters and you have small element types such as u8, consider using packed arrays (e.g. PackedByteArray) instead.

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

§

impl Element for f32

§

impl Element for f64

§

impl Element for i8

§

impl Element for i16

§

impl Element for i32

§

impl Element for i64

§

impl Element for u8

§

impl Element for u16

§

impl Element for u32

§

impl<T> Element for Option<Gd<T>>
where T: GodotClass,

§

impl<T, D> Element for Option<DynGd<T, D>>
where T: GodotClass, D: 'static + ?Sized,

Implementors§

§

impl Element for Rid

§

impl Element for Aabb

§

impl Element for AnyArray

§

impl Element for AnyDictionary

§

impl Element for Array<Variant>

§

impl Element for Basis

§

impl Element for Callable

§

impl Element for Color

§

impl Element for GString

§

impl Element for NodePath

§

impl Element for Plane

§

impl Element for Projection

§

impl Element for Quaternion

§

impl Element for Rect2

§

impl Element for Rect2i

§

impl Element for Signal

§

impl Element for StringName

§

impl Element for Transform2D

§

impl Element for Transform3D

§

impl Element for Variant

§

impl Element for Vector2

§

impl Element for Vector2i

§

impl Element for Vector3

§

impl Element for Vector3i

§

impl Element for Vector4

§

impl Element for Vector4i

§

impl<K, V> Element for Dictionary<K, V>
where K: Element, V: Element,

§

impl<T> Element for Gd<T>
where T: GodotClass,

§

impl<T> Element for PackedArray<T>
where T: PackedElement,

§

impl<T, D> Element for DynGd<T, D>
where T: GodotClass, D: 'static + ?Sized,