pub trait FromVariant {
    fn from_variant(variant: &Variant) -> Result<Self, FromVariantError>;
}
Expand description

Types that can be converted from a Variant. Conversions are performed in Rust, and can be implemented for custom types.

FromVariant generally avoids inexact conversions, favoring returning an error instead, with the noted exception of integer and float types that are not supported by Godot natively. This is stricter than what GDScript performs by default. For weakly-typed coercions of GDScript built-in typed, see CoerceFromVariant instead.

This trait is used for argument types of exported methods.

Option<T> and MaybeNot<T>

Option<T> requires the Variant to be T or Nil, in that order. For looser semantics, use MaybeNot<T>, which will catch all variant values that are not T as well.

Vec<T>

The FromVariant implementation for Vec<T> only allow homogeneous arrays. If you want to manually handle potentially heterogeneous values e.g. for error reporting, use VariantArray directly or compose with an appropriate wrapper: Vec<Option<T>> or Vec<MaybeNot<T>>.

Deriving FromVariant

The derive macro provides implementation consistent with derived ToVariant. See ToVariant for detailed documentation.

Required Methods

Implementations on Foreign Types

Expects a Variant populated with a Dictionary and tries to convert it into a HashMap.

Since Rust’s HashMap is unordered, there is no guarantee about the resulting element order. In fact it is possible that two program invocations cause a different output.

Expects a Variant populated with a VariantArray and tries to convert it into a HashSet.

Since Rust’s HashSet is unordered, there is no guarantee about the resulting element order. In fact it is possible that two program invocations cause a different output.

Implementors