Trait gdnative_core::core_types::ToVariant
source · 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, orNil
ifNone
Result<T, E>
is represented as an externally taggedDictionary
(see below).PhantomData<T>
is represented asNil
.&[T]
andVec<T>
are represented asVariantArray
s.FromVariant
is only implemented forVec<T>
.
Deriving ToVariant
The derive macro does the following mapping between Rust structures and Godot types:
Newtype(inner)
is unwrapped toinner
Tuple(a, b, c)
is represented as aVariantArray
([a, b, c]
)Struct { a, b, c }
is represented as aDictionary
({ "a": a, "b": b, "c": c }
)Unit
is represented as an emptyDictionary
({}
)Enum::Variant(a, b, c)
is represented as an externally taggedDictionary
({ "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§
fn to_variant(&self) -> Variant
Implementations on Foreign Types§
source§impl<'a, T> ToVariant for &'a Twhere
T: ToVariant + ?Sized,
impl<'a, T> ToVariant for &'a Twhere
T: ToVariant + ?Sized,
fn to_variant(&self) -> Variant
source§impl<'a, T> ToVariant for &'a mut Twhere
T: ToVariant + ?Sized,
impl<'a, T> ToVariant for &'a mut Twhere
T: ToVariant + ?Sized,
fn to_variant(&self) -> Variant
source§impl<T> ToVariant for PhantomData<T>
impl<T> ToVariant for PhantomData<T>
fn to_variant(&self) -> Variant
source§impl<T: ToVariant, E: ToVariant> ToVariant for Result<T, E>
impl<T: ToVariant, E: ToVariant> ToVariant for Result<T, E>
fn to_variant(&self) -> Variant
source§impl<K: ToVariant + Hash + ToVariantEq, V: ToVariant> ToVariant for HashMap<K, V>
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.
fn to_variant(&self) -> Variant
source§impl<T: ToVariant> ToVariant for HashSet<T>
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.