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

Creates a Variant from a value that implements ToVariant.

Creates an empty Variant.

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.

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.

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.

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.

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.

Returns this variant’s type.

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

Returns true if this is an empty variant.

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

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.