Struct gdnative_core::core_types::Variant
source · [−]pub struct Variant(_);Expand description
A Variant can represent all Godot values (core types or Object class instances).
The underlying data is either stored inline or reference-counted on the heap, depending on the size of the type and whether the it is trivially copyable.
If you compile godot-rust with the serde feature enabled, you will have
access to serialization/deserialization support: the traits Serialize
and Deserialize will be automatically implemented on VariantDispatch
as well as most of the types in core_types.
Implementations
sourceimpl Variant
impl Variant
sourcepub fn new<T: OwnedToVariant>(from: T) -> Self
pub fn new<T: OwnedToVariant>(from: T) -> Self
Creates a Variant from a value that implements ToVariant.
sourcepub fn to<T: FromVariant>(&self) -> Option<T>
pub fn to<T: FromVariant>(&self) -> Option<T>
Performs a strongly-typed, structure-aware conversion to T from this variant, if it
is a valid representation of said type. This is the same as T::from_variant(self).ok().
This is the same conversion used to parse arguments of exported methods. See
FromVariant for more details.
sourcepub fn try_to<T: FromVariant>(&self) -> Result<T, FromVariantError>
pub fn try_to<T: FromVariant>(&self) -> Result<T, FromVariantError>
Performs a strongly-typed, structure-aware conversion to T from this variant, if it
is a valid representation of said type. This is the same as T::from_variant(self).
This is the same conversion used to parse arguments of exported methods. See
FromVariant for more details.
sourcepub fn coerce_to<T: CoerceFromVariant>(&self) -> T
pub fn coerce_to<T: CoerceFromVariant>(&self) -> T
Coerce a value of type T out of this variant, through what Godot presents as a
“best-effort” conversion, possibly returning a default value.
See CoerceFromVariant for more details.
See also Variant::to and Variant::try_to for strongly-typed, structure-aware
conversions into Rust types.
sourcepub fn to_object<T>(&self) -> Option<Ref<T, Shared>>where
T: GodotObject,
pub fn to_object<T>(&self) -> Option<Ref<T, Shared>>where
T: GodotObject,
Convenience method to extract a Ref<T, Shared> from this variant, if the type
matches. This is the same as Ref::<T, Shared>::from_variant(self).ok().
This is the same conversion used to parse arguments of exported methods. See
FromVariant for more details.
sourcepub fn try_to_object<T>(&self) -> Result<Ref<T, Shared>, FromVariantError>where
T: GodotObject,
pub fn try_to_object<T>(&self) -> Result<Ref<T, Shared>, FromVariantError>where
T: GodotObject,
Convenience method to extract a Ref<T, Shared> from this variant, if the type
matches. This is the same as Ref::<T, Shared>::from_variant(self).
This is the same conversion used to parse arguments of exported methods. See
FromVariant for more details.
sourcepub fn get_type(&self) -> VariantType
pub fn get_type(&self) -> VariantType
Returns this variant’s type.
sourcepub fn dispatch(&self) -> VariantDispatch
pub fn dispatch(&self) -> VariantDispatch
Converts this variant to a primitive value depending on its type.
Examples
let variant = 42.to_variant();
let number_as_float = match variant.dispatch() {
VariantDispatch::I64(i) => i as f64,
VariantDispatch::F64(f) => f,
_ => panic!("not a number"),
};
approx::assert_relative_eq!(42.0, number_as_float);pub fn has_method(&self, method: impl Into<GodotString>) -> bool
sourcepub unsafe fn call(
&mut self,
method: impl Into<GodotString>,
args: &[Variant]
) -> Result<Variant, CallError>
pub unsafe fn call(
&mut self,
method: impl Into<GodotString>,
args: &[Variant]
) -> Result<Variant, CallError>
Invokes a method on the held object.
Safety
This method may invoke [Object::call()] internally, which is unsafe, as it allows execution of arbitrary code (including user-defined code in GDScript or unsafe Rust).
Trait Implementations
sourceimpl<'a> From<&'a Variant> for VariantDispatch
impl<'a> From<&'a Variant> for VariantDispatch
sourceimpl<'a> From<&'a VariantDispatch> for Variant
impl<'a> From<&'a VariantDispatch> for Variant
sourcefn from(v: &'a VariantDispatch) -> Self
fn from(v: &'a VariantDispatch) -> Self
sourceimpl FromVariant for Variant
impl FromVariant for Variant
fn from_variant(variant: &Variant) -> Result<Self, FromVariantError>
sourceimpl Ord for Variant
impl Ord for Variant
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl PartialOrd<Variant> for Variant
impl PartialOrd<Variant> for Variant
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl Eq for Variant
impl ToVariantEq for Variant
Auto Trait Implementations
impl RefUnwindSafe for Variant
impl Send for Variant
impl Sync for Variant
impl Unpin for Variant
impl UnwindSafe for Variant
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.