Struct Dynamic

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn id(&self) -> TypeId

Read the type id for the contained value.

Source

pub fn is<T: Any>(&self) -> bool

Check if the contained type is a T.

Source

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

Source

pub fn downcast_ref<T: Any>(&self) -> Option<&T>

If the contained value is a T, get an immutable reference to it.

Source

pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>

If the contained value is a T, get a mutable reference to it.

Trait Implementations§

Source§

impl Debug for Dynamic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Dynamic

§

impl !RefUnwindSafe for Dynamic

§

impl !Send for Dynamic

§

impl !Sized for Dynamic

§

impl !Sync for Dynamic

§

impl !Unpin for Dynamic

§

impl !UnwindSafe for Dynamic

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more