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§

source§

impl Variant

source

pub fn new<T>(from: T) -> Variantwhere
    T: OwnedToVariant,

Creates a Variant from a value that implements ToVariant.

source

pub fn nil() -> Variant

Creates an empty Variant.

source

pub fn to<T>(&self) -> Option<T>where
    T: FromVariant,

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.

source

pub fn try_to<T>(&self) -> Result<T, FromVariantError>where
    T: FromVariant,

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.

source

pub fn coerce_to<T>(&self) -> Twhere
    T: CoerceFromVariant,

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.

source

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.

source

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.

source

pub fn get_type(&self) -> VariantType

Returns this variant’s type.

source

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);
source

pub fn is_nil(&self) -> bool

Returns true if this is an empty variant.

source

pub fn has_method(&self, method: impl Into<GodotString>) -> bool

source

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).

source

pub fn evaluate(
    &self,
    op: VariantOperator,
    rhs: &Variant
) -> Result<Variant, InvalidOp>

Evaluates a variant operator on self and rhs and returns the result on success.

Errors

Returns Err(InvalidOp) if the result is not valid.

Trait Implementations§

source§

impl Clone for Variant

source§

fn clone(&self) -> Variant

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Variant

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Variant

source§

fn default() -> Variant

Returns the “default value” for a type. Read more
source§

impl Display for Variant

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Drop for Variant

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> From<&'a Variant> for VariantDispatch

source§

fn from(v: &'a Variant) -> VariantDispatch

Converts to this type from the input type.
source§

impl<'a> From<&'a VariantDispatch> for Variant

source§

fn from(v: &'a VariantDispatch) -> Variant

Converts to this type from the input type.
source§

impl FromVariant for Variant

source§

impl Ord for Variant

source§

fn cmp(&self, other: &Variant) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Variant> for Variant

source§

fn eq(&self, other: &Variant) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Variant> for Variant

source§

fn partial_cmp(&self, other: &Variant) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ToVariant for Variant

source§

impl Eq for Variant

source§

impl ToVariantEq for Variant

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere
    Q: Eq + ?Sized,
    K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> OwnedToVariant for Twhere
    T: ToVariant,

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere
    T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.