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

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.