pub trait ToVariant {
    fn to_variant(&self) -> Variant;
}
Expand description

Types that can be converted to a Variant.

Wrappers and collections

Implementations are provided for a few common Rust wrappers and collections:

  • Option<T> is unwrapped to inner value, or Nil if None
  • Result<T, E> is represented as an externally tagged Dictionary (see below).
  • PhantomData<T> is represented as Nil.
  • &[T] and Vec<T> are represented as VariantArrays. FromVariant is only implemented for Vec<T>.

Deriving ToVariant

The derive macro does the following mapping between Rust structures and Godot types:

  • Newtype(inner) is unwrapped to inner
  • Tuple(a, b, c) is represented as a VariantArray ([a, b, c])
  • Struct { a, b, c } is represented as a Dictionary ({ "a": a, "b": b, "c": c })
  • Unit is represented as an empty Dictionary ({})
  • Enum::Variant(a, b, c) is represented as an externally tagged Dictionary ({ "Variant": [a, b, c] }), unless another representation is specified with #[variant(enum)] (see below).

Behavior of the derive macros can be customized using attributes:

Item attributes

  • #[variant(enum = "str")]

Only applicable to field-less enums. Variants of types annotated with this attribute are represented as stringified values of their names, i.e. "Variant" for Enum::Variant.

  • #[variant(enum = "repr")]

Only applicable to field-less enums with a explicit primitive #[repr] type. Variants of types annotated with this attribute are represented as their primitive integral values.

Field attributes

  • #[variant(to_variant_with = "path::to::func")]

Use the given function to convert the field to Variant. The function’s signature is expected to be fn(&T) -> Variant, although it can be generic over T.

  • #[variant(from_variant_with = "path::to::func")]

Use the given function to convert from a Variant. The function’s signature is expected to be fn(&Variant) -> Result<T, FromVariantError>, although it can be generic over T.

  • #[variant(with = "path::to::mod")]

Convenience attribute that sets to_variant_with to path::to::mod::to_variant and from_variant_with to path::to::mod::from_variant.

  • #[variant(skip_to_variant)]

Skip the field when converting to Variant.

  • #[variant(skip_from_variant)]

Skip the field when converting from Variant. A default vale will be obtained using Default::default().

  • #[variant(skip)]

Convenience attribute that sets skip_to_variant and skip_from_variant.

Required Methods§

Implementations on Foreign Types§

source§

impl ToVariant for ()

source§

impl<'a, T> ToVariant for &'a Twhere
    T: ToVariant + ?Sized,

source§

impl<'a, T> ToVariant for &'a mut Twhere
    T: ToVariant + ?Sized,

source§

impl ToVariant for i64

source§

impl ToVariant for u64

source§

impl ToVariant for bool

source§

impl ToVariant for f64

source§

impl ToVariant for i8

source§

impl ToVariant for i16

source§

impl ToVariant for i32

source§

impl ToVariant for isize

source§

impl ToVariant for u8

source§

impl ToVariant for u16

source§

impl ToVariant for u32

source§

impl ToVariant for usize

source§

impl ToVariant for f32

source§

impl ToVariant for str

source§

impl ToVariant for String

source§

impl<T> ToVariant for PhantomData<T>

source§

impl<T: ToVariant> ToVariant for Option<T>

source§

impl<T: ToVariant, E: ToVariant> ToVariant for Result<T, E>

source§

impl<T: ToVariant> ToVariant for &[T]

source§

impl<T: ToVariant> ToVariant for Vec<T>

source§

impl<K: ToVariant + Hash + ToVariantEq, V: ToVariant> ToVariant for HashMap<K, V>

Converts the hash map to a Dictionary, wrapped in a Variant.

Note that Rust’s HashMap is non-deterministically ordered for security reasons, meaning that the order of the same elements will differ between two program invocations. To provide a deterministic output in Godot (e.g. UI elements for properties), the elements are sorted by key.

source§

impl<T: ToVariant> ToVariant for HashSet<T>

Converts the hash set to a VariantArray, wrapped in a Variant.

Note that Rust’s HashSet is non-deterministically ordered for security reasons, meaning that the order of the same elements will differ between two program invocations. To provide a deterministic output in Godot (e.g. UI elements for properties), the elements are sorted by key.

source§

impl<T1: ToVariant, T2: ToVariant, T3: ToVariant, T4: ToVariant, T5: ToVariant, T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

source§

impl<T2: ToVariant, T3: ToVariant, T4: ToVariant, T5: ToVariant, T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

source§

impl<T3: ToVariant, T4: ToVariant, T5: ToVariant, T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

source§

impl<T4: ToVariant, T5: ToVariant, T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T4, T5, T6, T7, T8, T9, T10, T11, T12)

source§

impl<T5: ToVariant, T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T5, T6, T7, T8, T9, T10, T11, T12)

source§

impl<T6: ToVariant, T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T6, T7, T8, T9, T10, T11, T12)

source§

impl<T7: ToVariant, T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T7, T8, T9, T10, T11, T12)

source§

impl<T8: ToVariant, T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T8, T9, T10, T11, T12)

source§

impl<T9: ToVariant, T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T9, T10, T11, T12)

source§

impl<T10: ToVariant, T11: ToVariant, T12: ToVariant> ToVariant for (T10, T11, T12)

source§

impl<T11: ToVariant, T12: ToVariant> ToVariant for (T11, T12)

source§

impl<T12: ToVariant> ToVariant for (T12,)

Implementors§