Struct sub_script::engine::Dynamic
source · [−]pub struct Dynamic(_);
Expand description
Dynamic type containing any value.
Implementations
sourceimpl 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>
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.
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>
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.
sourceimpl Dynamic
impl Dynamic
sourcepub const fn is_variant(&self) -> bool
pub const fn is_variant(&self) -> bool
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
.
sourceimpl Dynamic
impl Dynamic
sourcepub const UNIT: Dynamic = Self(Union::Unit((), DEFAULT_TAG_VALUE, ReadWrite))
pub const UNIT: Dynamic = Self(Union::Unit((), DEFAULT_TAG_VALUE, ReadWrite))
A Dynamic
containing a ()
.
sourcepub const MILLION: Dynamic = Self::from_int(1000000)
pub const MILLION: Dynamic = Self::from_int(1000000)
A Dynamic
containing the integer 1,000,000.
sourcepub const NEGATIVE_ONE: Dynamic = Self::from_int(-1)
pub const NEGATIVE_ONE: Dynamic = Self::from_int(-1)
A Dynamic
containing the integer -1.
sourcepub const NEGATIVE_TWO: Dynamic = Self::from_int(-2)
pub const NEGATIVE_TWO: Dynamic = Self::from_int(-2)
A Dynamic
containing the integer -2.
sourcepub fn from_decimal(value: Decimal) -> Dynamic
pub fn from_decimal(value: Decimal) -> Dynamic
sourcepub fn from_map(map: BTreeMap<SmartString<LazyCompact>, Dynamic>) -> Dynamic
pub fn from_map(map: BTreeMap<SmartString<LazyCompact>, Dynamic>) -> Dynamic
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.
If a &mut Dynamic
to such a constant is passed to a Rust function, the function
can use this information to return an error of
ErrorAssignmentToConstant
if its value
is going to be modified.
This safe-guards constant values from being modified from within Rust functions.
sourcepub fn from<T>(value: T) -> Dynamic where
T: Variant + Clone,
pub fn from<T>(value: T) -> Dynamic where
T: Variant + Clone,
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
Into<Dynamic>
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 Into<Dynamic>
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.
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 where
T: Any + Clone,
pub fn cast<T>(self) -> T where
T: Any + Clone,
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 where
T: Any + Clone,
pub fn clone_cast<T>(&self) -> T where
T: Any + Clone,
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>> where
T: Any + Clone,
pub fn read_lock<T>(&self) -> Option<DynamicReadLock<'_, T>> where
T: Any + Clone,
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>> where
T: Any + Clone,
pub fn write_lock<T>(&mut self) -> Option<DynamicWriteLock<'_, T>> where
T: Any + Clone,
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 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 as_decimal(&self) -> Result<Decimal, &'static str>
pub fn as_decimal(&self) -> Result<Decimal, &'static str>
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
sourceimpl<'d> Deserialize<'d> for Dynamic
impl<'d> Deserialize<'d> for Dynamic
sourcefn deserialize<D>(de: D) -> Result<Dynamic, <D as Deserializer<'d>>::Error> where
D: Deserializer<'d>,
fn deserialize<D>(de: D) -> Result<Dynamic, <D as Deserializer<'d>>::Error> where
D: Deserializer<'d>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<'_> From<&'_ ImmutableString> for Dynamic
impl<'_> From<&'_ ImmutableString> for Dynamic
sourcefn from(value: &ImmutableString) -> Dynamic
fn from(value: &ImmutableString) -> Dynamic
Converts to this type from the input type.
sourceimpl<K, T> From<BTreeMap<K, T>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
T: Variant + Clone,
impl<K, T> From<BTreeMap<K, T>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
T: Variant + Clone,
sourceimpl<K> From<BTreeSet<K>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
impl<K> From<BTreeSet<K>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
sourceimpl<K, T> From<HashMap<K, T, RandomState>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
T: Variant + Clone,
impl<K, T> From<HashMap<K, T, RandomState>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
T: Variant + Clone,
sourcefn from(value: HashMap<K, T, RandomState>) -> Dynamic
fn from(value: HashMap<K, T, RandomState>) -> Dynamic
Converts to this type from the input type.
sourceimpl<K> From<HashSet<K, RandomState>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
impl<K> From<HashSet<K, RandomState>> for Dynamic where
K: Into<SmartString<LazyCompact>>,
sourcefn from(value: HashSet<K, RandomState>) -> Dynamic
fn from(value: HashSet<K, RandomState>) -> Dynamic
Converts to this type from the input type.
sourceimpl From<RangeInclusive<i64>> for Dynamic
impl From<RangeInclusive<i64>> for Dynamic
sourcefn from(value: RangeInclusive<i64>) -> Dynamic
fn from(value: RangeInclusive<i64>) -> Dynamic
Converts to this type from the input type.
sourceimpl<S> From<S> for Dynamic where
S: Into<ImmutableString>,
impl<S> From<S> for Dynamic where
S: Into<ImmutableString>,
sourceimpl<T> FromIterator<T> for Dynamic where
T: Variant + Clone,
impl<T> FromIterator<T> for Dynamic where
T: Variant + Clone,
sourcefn from_iter<X>(iter: X) -> Dynamic where
X: IntoIterator<Item = T>,
fn from_iter<X>(iter: X) -> Dynamic where
X: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
sourceimpl Serialize for Dynamic
impl Serialize for Dynamic
sourcefn serialize<S>(
&self,
ser: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
fn serialize<S>(
&self,
ser: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
Auto Trait Implementations
impl !RefUnwindSafe for Dynamic
impl Send for Dynamic
impl Sync for Dynamic
impl Unpin for Dynamic
impl !UnwindSafe for Dynamic
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> CheckedConversion for T
impl<T> CheckedConversion for T
sourcefn checked_from<T>(t: T) -> Option<Self> where
Self: TryFrom<T>,
fn checked_from<T>(t: T) -> Option<Self> where
Self: TryFrom<T>,
Convert from a value of T
into an equivalent instance of Option<Self>
. Read more
sourcefn checked_into<T>(self) -> Option<T> where
Self: TryInto<T>,
fn checked_into<T>(self) -> Option<T> where
Self: TryInto<T>,
Consume self to return Some
equivalent value of Option<T>
. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T, Outer> IsWrappedBy<Outer> for T where
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T, Outer> IsWrappedBy<Outer> for T where
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
Convert from a value of T
into an equivalent instance of Self
. Read more
fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
Consume self to return an equivalent value of T
. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<S, T> UncheckedInto<T> for S where
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for S where
T: UncheckedFrom<S>,
sourcefn unchecked_into(self) -> T
fn unchecked_into(self) -> T
The counterpart to unchecked_from
.
impl<T, S> UniqueSaturatedInto<T> for S where
T: Bounded,
S: TryInto<T>,
impl<T, S> UniqueSaturatedInto<T> for S where
T: Bounded,
S: TryInto<T>,
fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
Consume self to return an equivalent value of T
.
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more