pub struct Dynamic { /* private fields */ }
Expand description
A dynamically typed value.
Differs from Any
in that it pre-computes type information at
creation-time, so that downcasting and other queries to the type
information can be implemented without virtual calls.
Not Sized, since the size of the type is determined at runtime, so must be
used behind a pointer (e.g. &Dynamic
, Box<Dynamic>
, etc.)
Implementations§
Source§impl Dynamic
impl Dynamic
Sourcepub fn new<T: Any>(val: T) -> Box<Dynamic>
pub fn new<T: Any>(val: T) -> Box<Dynamic>
Create a new, heap-allocated Dynamic value containing the given value.
The resulting Dynamic
can be downcasted back to a T
.
Sourcepub fn from_ref<T: Any>(val: &Described<T>) -> &Dynamic
pub fn from_ref<T: Any>(val: &Described<T>) -> &Dynamic
Create a new, immutable Dynamic value from the given described reference.
The resulting Dynamic
can be downcasted back to a T
.
Sourcepub fn from_mut<T: Any>(val: &mut Described<T>) -> &mut Dynamic
pub fn from_mut<T: Any>(val: &mut Described<T>) -> &mut Dynamic
Create a new, mutable Dynamic value from the given described reference.
The resulting Dynamic
can be downcasted back to a T
.
Sourcepub fn downcast<T: Any>(self: Box<Self>) -> Result<Box<Described<T>>, Box<Self>>
pub fn downcast<T: Any>(self: Box<Self>) -> Result<Box<Described<T>>, Box<Self>>
If the contained value is a T
, downcast back to it.
If the value is not a T
, returns Err(self)
.
Sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
If the contained value is a T
, get an immutable reference to it.
Sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
If the contained value is a T
, get a mutable reference to it.