pub struct Dynamic(_);
Expand description

Dynamic type containing any value.

Implementations

👎Deprecated since 1.1.0:

use into_string instead

Convert the Dynamic into a String and return it. If there are other references to the same string, a cloned copy is returned. Returns the name of the actual type if the cast fails.

Deprecated

This method is deprecated. Use into_string instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0:

use into_immutable_string instead

Convert the Dynamic into an ImmutableString and return it. Returns the name of the actual type if the cast fails.

Deprecated

This method is deprecated. Use into_immutable_string instead.

This method will be removed in the next major version.

Get the arbitrary data attached to this Dynamic.

Attach arbitrary data to this Dynamic.

Does this Dynamic hold a variant data type instead of one of the supported system primitive types?

Is the value held by this Dynamic shared?

Not available under no_closure.

Is the value held by this Dynamic a particular type?

If the Dynamic is a shared variant checking is performed on top of its internal value.

Get the TypeId of the value held by this Dynamic.

Panics or Deadlocks When Value is Shared

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

Get the name of the type of the value held by this Dynamic.

Panics or Deadlocks When Value is Shared

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

A Dynamic containing a ().

A Dynamic containing a true.

A Dynamic containing a false.

A Dynamic containing the integer zero.

A Dynamic containing the integer 1.

A Dynamic containing the integer 2.

A Dynamic containing the integer 3.

A Dynamic containing the integer 10.

A Dynamic containing the integer 100.

A Dynamic containing the integer 1,000.

A Dynamic containing the integer 1,000,000.

A Dynamic containing the integer -1.

A Dynamic containing the integer -2.

A Dynamic containing 0.0.

Not available under no_float.

A Dynamic containing 1.0.

Not available under no_float.

A Dynamic containing 2.0.

Not available under no_float.

A Dynamic containing 10.0.

Not available under no_float.

A Dynamic containing 100.0.

Not available under no_float.

A Dynamic containing 1000.0.

Not available under no_float.

A Dynamic containing 1000000.0.

Not available under no_float.

A Dynamic containing -1.0.

Not available under no_float.

A Dynamic containing -2.0.

Not available under no_float.

A Dynamic containing 0.5.

Not available under no_float.

A Dynamic containing 0.25.

Not available under no_float.

A Dynamic containing 0.2.

Not available under no_float.

A Dynamic containing 0.1.

Not available under no_float.

A Dynamic containing 0.01.

Not available under no_float.

A Dynamic containing 0.001.

Not available under no_float.

A Dynamic containing 0.000001.

Not available under no_float.

A Dynamic containing π.

Not available under no_float.

A Dynamic containing π/2.

Not available under no_float.

A Dynamic containing π/4.

Not available under no_float.

A Dynamic containing 2π.

Not available under no_float.

A Dynamic containing 1/π.

Not available under no_float.

A Dynamic containing e.

Not available under no_float.

A Dynamic containing log e.

Not available under no_float.

A Dynamic containing ln 10.

Not available under no_float.

Create a new Dynamic from a bool.

Create a new Dynamic from an INT.

Create a new Dynamic from a char.

Create a new Dynamic from a FLOAT.

Not available under no_float.

Create a new Dynamic from a Decimal.

Exported under the decimal feature only.

Create a Dynamic from an Array.

Create a Dynamic from a Blob.

Create a Dynamic from a Map.

Create a new Dynamic from an Instant.

Not available under no-std.

Make this Dynamic read-only (i.e. a constant).

Is this Dynamic read-only?

Constant Dynamic values are read-only.

Usage

If a &mut Dynamic to such a constant is passed to a Rust function, the function can use this information to return the error ErrorAssignmentToConstant if its value will be modified.

This safe-guards constant values from being modified within Rust functions.

Shared Values

If a Dynamic holds a shared value, then it is read-only only if the shared value itself is read-only.

Create a Dynamic from any type. A Dynamic value is simply returned as is.

Notes

Beware that you need to pass in an Array type for it to be recognized as an Array. A Vec<T> does not get automatically converted to an Array, but will be a custom type instead (stored as a trait object). Use Dynamic::from_array to convert a Vec<T> into a Dynamic as an Array value.

Similarly, passing in a HashMap<String, T> or BTreeMap<String, T> will not get a Map but a custom type. Again, use Dynamic::from_map to get a Dynamic with a Map value.

Examples
use rhai::Dynamic;

let result = Dynamic::from(42_i64);
assert_eq!(result.type_name(), "i64");
assert_eq!(result.to_string(), "42");

let result = Dynamic::from("hello");
assert_eq!(result.type_name(), "string");
assert_eq!(result.to_string(), "hello");

let new_result = Dynamic::from(result);
assert_eq!(new_result.type_name(), "string");
assert_eq!(new_result.to_string(), "hello");

Turn the Dynamic value into a shared Dynamic value backed by an Rc<RefCell<Dynamic>> or Arc<RwLock<Dynamic>> depending on the sync feature.

Not available under no_closure.

Shared Dynamic values are relatively cheap to clone as they simply increment the reference counts.

Shared Dynamic values can be converted seamlessly to and from ordinary Dynamic values.

If the Dynamic value is already shared, this method returns itself.

Convert the Dynamic value into specific type.

Casting to a Dynamic just returns as is, but if it contains a shared value, it is cloned into a Dynamic with a normal value.

Returns None if types mismatched.

Panics or Deadlocks

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

These normally shouldn’t occur since most operations in Rhai is single-threaded.

Example
use rhai::Dynamic;

let x = Dynamic::from(42_u32);

assert_eq!(x.try_cast::<u32>().expect("x should be u32"), 42);

Convert the Dynamic value into a specific type.

Casting to a Dynamic just returns as is, but if it contains a shared value, it is cloned into a Dynamic with a normal value.

Panics or Deadlocks

Panics if the cast fails (e.g. the type of the actual value is not the same as the specified type).

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

These normally shouldn’t occur since most operations in Rhai is single-threaded.

Example
use rhai::Dynamic;

let x = Dynamic::from(42_u32);

assert_eq!(x.cast::<u32>(), 42);

Clone the Dynamic value and convert it into a specific type.

Casting to a Dynamic just returns as is, but if it contains a shared value, it is cloned into a Dynamic with a normal value.

Returns None if types mismatched.

Panics or Deadlocks

Panics if the cast fails (e.g. the type of the actual value is not the same as the specified type).

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

These normally shouldn’t occur since most operations in Rhai is single-threaded.

Example
use rhai::Dynamic;

let x = Dynamic::from(42_u32);
let y = &x;

assert_eq!(y.clone_cast::<u32>(), 42);

Flatten the Dynamic and clone it.

If the Dynamic is not a shared value, it returns a cloned copy.

If the Dynamic is a shared value, it returns a cloned copy of the shared value.

Flatten the Dynamic.

If the Dynamic is not a shared value, it returns itself.

If the Dynamic is a shared value, it returns the shared value if there are no outstanding references, or a cloned copy.

Is the Dynamic a shared value that is locked?

Not available under no_closure.

Note

Under the sync feature, shared values use RwLock and they are never locked. Access just waits until the RwLock is released. So this method always returns false under Sync.

Get a reference of a specific type to the Dynamic. Casting to Dynamic just returns a reference to it.

Returns None if the cast fails.

Panics or Deadlocks When Value is Shared

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

Get a mutable reference of a specific type to the Dynamic. Casting to Dynamic just returns a mutable reference to it.

Returns None if the cast fails.

Panics or Deadlocks When Value is Shared

Under the sync feature, this call may deadlock, or panic. Otherwise, this call panics if the data is currently borrowed for write.

Cast the Dynamic as a unit (). Returns the name of the actual type if the cast fails.

Cast the Dynamic as the system integer type INT. Returns the name of the actual type if the cast fails.

Cast the Dynamic as the system floating-point type FLOAT. Returns the name of the actual type if the cast fails.

Not available under no_float.

(decimal) Cast the Dynamic as a Decimal. Returns the name of the actual type if the cast fails.

Exported under the decimal feature only.

Cast the Dynamic as a bool. Returns the name of the actual type if the cast fails.

Cast the Dynamic as a char. Returns the name of the actual type if the cast fails.

Convert the Dynamic into a String. If there are other references to the same string, a cloned copy is returned. Returns the name of the actual type if the cast fails.

Convert the Dynamic into an ImmutableString. Returns the name of the actual type if the cast fails.

Convert the Dynamic into an Array. Returns the name of the actual type if the cast fails.

Convert the Dynamic into a Vec. Returns the name of the actual type if any cast fails.

Convert the Dynamic into a Blob. Returns the name of the actual type if the cast fails.

Trait Implementations

Clone the Dynamic value.

WARNING

The cloned copy is marked read-write even if the original is read-only.

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
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more

Hash the Dynamic value.

Panics

Panics if the Dynamic value contains an unrecognized trait object.

Feeds a slice of this type into the given Hasher. Read more
Serialize this value into the given Serde serializer. 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

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.