pub trait Any: 'static {
// Required method
fn type_id(&self) -> TypeId;
}
Expand description
A trait to emulate dynamic typing.
Most types implement Any
. However, any type which contains a non-'static
reference does not.
See the module-level documentation for more details.
Required Methods§
Implementations§
Source§impl dyn Any
impl dyn Any
1.0.0 · Sourcepub fn is<T: Any>(&self) -> bool
pub fn is<T: Any>(&self) -> bool
Returns true
if the inner type is the same as T
.
§Examples
1.0.0 · Sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
Returns some reference to the inner value if it is of type T
, or
None
if it isn’t.
§Examples
1.0.0 · Sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
Returns some mutable reference to the inner value if it is of type T
, or
None
if it isn’t.
§Examples
Sourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)Returns a reference to the inner value as type dyn T
.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
}
§Safety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
Sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
downcast_unchecked
#90850)Returns a mutable reference to the inner value as type dyn T
.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let mut x: Box<dyn Any> = Box::new(1_usize);
unsafe {
*x.downcast_mut_unchecked::<usize>() += 1;
}
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
§Safety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
Source§impl dyn Any + Send
impl dyn Any + Send
1.0.0 · Sourcepub fn is<T: Any>(&self) -> bool
pub fn is<T: Any>(&self) -> bool
Forwards to the method defined on the type dyn Any
.
§Examples
1.0.0 · Sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
Forwards to the method defined on the type dyn Any
.
§Examples
1.0.0 · Sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
Forwards to the method defined on the type dyn Any
.
§Examples
Sourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)Sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
downcast_unchecked
#90850)Forwards to the method defined on the type dyn Any
.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let mut x: Box<dyn Any> = Box::new(1_usize);
unsafe {
*x.downcast_mut_unchecked::<usize>() += 1;
}
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
§Safety
Same as the method on the type dyn Any
.
Source§impl dyn Any + Send + Sync
impl dyn Any + Send + Sync
1.28.0 · Sourcepub fn is<T: Any>(&self) -> bool
pub fn is<T: Any>(&self) -> bool
Forwards to the method defined on the type Any
.
§Examples
1.28.0 · Sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
Forwards to the method defined on the type Any
.
§Examples
1.28.0 · Sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
Forwards to the method defined on the type Any
.
§Examples
Sourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)Forwards to the method defined on the type Any
.