pub struct Dynamic(/* private fields */);
Expand description
Dynamic type containing any value.
Implementations§
source§impl Dynamic
impl Dynamic
sourcepub fn as_string(self) -> Result<String, &'static str>
👎Deprecated since 1.1.0: use into_string
instead
pub fn as_string(self) -> Result<String, &'static str>
into_string
insteadConvert 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.
sourcepub fn as_immutable_string(self) -> Result<ImmutableString, &'static str>
👎Deprecated since 1.1.0: use into_immutable_string
instead
pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str>
into_immutable_string
insteadConvert 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.
source§impl Dynamic
impl Dynamic
source§impl Dynamic
impl Dynamic
sourcepub const NEGATIVE_ONE: Dynamic = _
pub const NEGATIVE_ONE: Dynamic = _
A Dynamic
containing the integer -1.
sourcepub const NEGATIVE_TWO: Dynamic = _
pub const NEGATIVE_TWO: Dynamic = _
A Dynamic
containing the integer -2.
sourcepub fn from_timestamp(value: Instant) -> Dynamic
pub fn from_timestamp(value: Instant) -> Dynamic
sourcepub fn into_read_only(self) -> Dynamic
pub fn into_read_only(self) -> Dynamic
Make this Dynamic
read-only (i.e. a constant).
sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
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.
sourcepub fn from<T>(value: T) -> Dynamicwhere
T: Variant + Clone,
pub fn from<T>(value: T) -> Dynamicwhere
T: Variant + Clone,
Create a Dynamic
from any type. A Dynamic
value is simply returned as is.
§Arrays
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 array.into()
or array.into_iter()
to convert a Vec<T>
into a Dynamic
as
an Array
value. See the examples for details.
§Hash Maps
Similarly, passing in a HashMap<String, T>
or
BTreeMap<String, T>
will not get a Map
but a
custom type.
Again, use map.into()
to get a Dynamic
with a Map
value.
See the examples for details.
§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");
// Arrays - this is a custom object!
let result = Dynamic::from(vec![1_i64, 2, 3]);
assert_eq!(result.type_name(), "alloc::vec::Vec<i64>");
// Use '.into()' to convert a Vec<T> into an Array
let result: Dynamic = vec![1_i64, 2, 3].into();
assert_eq!(result.type_name(), "array");
// Hash map
let mut map = HashMap::new();
map.insert("a".to_string(), 1_i64);
// This is a custom object!
let result = Dynamic::from(map.clone());
assert_eq!(result.type_name(), "std::collections::hash::map::HashMap<alloc::string::String, i64>");
// Use '.into()' to convert a HashMap<String, T> into an object map
let result: Dynamic = map.into();
assert_eq!(result.type_name(), "map");
sourcepub fn take(&mut self) -> Dynamic
pub fn take(&mut self) -> Dynamic
Return this Dynamic
, replacing it with Dynamic::UNIT
.
sourcepub fn try_cast<T>(self) -> Option<T>where
T: Any,
pub fn try_cast<T>(self) -> Option<T>where
T: Any,
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);
sourcepub fn cast<T>(self) -> T
pub fn cast<T>(self) -> T
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);
sourcepub fn clone_cast<T>(&self) -> T
pub fn clone_cast<T>(&self) -> T
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);
sourcepub fn flatten_clone(&self) -> Dynamic
pub fn flatten_clone(&self) -> Dynamic
sourcepub fn read_lock<T>(&self) -> Option<DynamicReadLock<'_, T>>
pub fn read_lock<T>(&self) -> Option<DynamicReadLock<'_, T>>
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.
sourcepub fn write_lock<T>(&mut self) -> Option<DynamicWriteLock<'_, T>>
pub fn write_lock<T>(&mut self) -> Option<DynamicWriteLock<'_, T>>
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.
sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Return true
if the Dynamic
holds an ImmutableString
.
sourcepub fn is_timestamp(&self) -> bool
pub fn is_timestamp(&self) -> bool
sourcepub fn as_unit(&self) -> Result<(), &'static str>
pub fn as_unit(&self) -> Result<(), &'static str>
Cast the Dynamic
as a unit ()
.
Returns the name of the actual type if the cast fails.
sourcepub fn into_string(self) -> Result<String, &'static str>
pub fn into_string(self) -> Result<String, &'static str>
sourcepub fn into_immutable_string(self) -> Result<ImmutableString, &'static str>
pub fn into_immutable_string(self) -> Result<ImmutableString, &'static str>
Convert the Dynamic
into an ImmutableString
.
Returns the name of the actual type if the cast fails.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Dynamic
impl<'de> Deserialize<'de> for Dynamic
source§fn deserialize<D>(
deserializer: D
) -> Result<Dynamic, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<Dynamic, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl Extend<Dynamic> for FnPtr
impl Extend<Dynamic> for FnPtr
source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = Dynamic>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = Dynamic>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)