Struct rhai::Dynamic[][src]

pub struct Dynamic(_);
Expand description

Dynamic type containing any value.

Implementations

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

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

Is the value held by this Dynamic a particular type?

If the Dynamic is a Shared variant checking is performed on top of it’s 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.

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

Safety

This type uses some unsafe code, mainly for type casting.

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 generic restricted trait object instead, because Vec<T> is not a supported standard type.

Similarly, passing in a HashMap<String, T> will not get a Map but a trait object.

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.

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.

Panics

Panics under the no_closure feature.

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>().unwrap(), 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.

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

assert_eq!(x.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 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?

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 the system integer type INT and return it. Returns the name of the actual type if the cast fails.

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

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

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

Cast the Dynamic as a string and return the string slice. Returns the name of the actual type if the cast fails.

Cast is failing if self is Shared Dynamic

Convert the Dynamic into 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.

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

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

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

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

Performs the conversion.

Performs the conversion.

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)

recently added

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.