Trait Element
pub trait Element:
ToGodot
+ FromGodot
+ 'static { }Expand description
Marker trait to identify types that can be stored in Array<T> and Dictionary<K, V>.
Implemented for most types that can interact with Godot. A notable exception is Array<T> and Dictionary<K, V> – Godot doesn’t support
typed collections to be nested. You can still store typed collections, but you need to use AnyArray and
AnyDictionary, which can be either typed or untyped. We also don’t support VarArray and
VarDictionary (special case of the former with T=Variant), because godot-rust cannot statically guarantee that the nested collections
are indeed untyped. In a GDScript Array[Array], you can store both typed and untyped arrays, even within the same collection.
See also ElementType for a runtime representation of this.
§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.