Trait gdnative::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] }
)
Behavior of the derive macros can be customized using attributes:
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
.