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>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
Returns true if the inner type is the same as T.
§Examples
1.0.0 · Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
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>(&mut self) -> Option<&mut T>where
T: Any,
pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
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_unchecked_ref<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_ref<T>(&self) -> &Twhere
T: Any,
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_unchecked_ref::<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_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
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_unchecked_mut::<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>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
Forwards to the method defined on the type dyn Any.
§Examples
1.0.0 · Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
Forwards to the method defined on the type dyn Any.
§Examples
1.0.0 · Sourcepub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
Forwards to the method defined on the type dyn Any.
§Examples
Sourcepub unsafe fn downcast_unchecked_ref<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_ref<T>(&self) -> &Twhere
T: Any,
downcast_unchecked #90850)Forwards to the method defined on the type dyn Any.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_unchecked_ref::<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_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
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_unchecked_mut::<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 + Sync + Send
impl dyn Any + Sync + Send
1.28.0 · Sourcepub fn is<T>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
Forwards to the method defined on the type Any.
§Examples
1.28.0 · Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
Forwards to the method defined on the type Any.
§Examples
1.28.0 · Sourcepub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
Forwards to the method defined on the type Any.
§Examples
Sourcepub unsafe fn downcast_unchecked_ref<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_ref<T>(&self) -> &Twhere
T: Any,
downcast_unchecked #90850)Forwards to the method defined on the type Any.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_unchecked_ref::<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_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked_mut<T>(&mut self) -> &mut Twhere
T: Any,
downcast_unchecked #90850)Forwards to the method defined on the type Any.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let mut x: Box<dyn Any> = Box::new(1_usize);
unsafe {
*x.downcast_unchecked_mut::<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<A> Box<dyn Any, A>where
A: Allocator,
impl<A> Box<dyn Any, A>where
A: Allocator,
1.0.0 · Sourcepub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any, A>>where
T: Any,
pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any, A>>where
T: Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative see downcast.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}§Safety
The contained value must be of type T. Calling this method
with the incorrect type is undefined behavior.
Source§impl<A> Box<dyn Any + Send, A>where
A: Allocator,
impl<A> Box<dyn Any + Send, A>where
A: Allocator,
1.0.0 · Sourcepub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Send, A>>where
T: Any,
pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Send, A>>where
T: Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative see downcast.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any + Send> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}§Safety
The contained value must be of type T. Calling this method
with the incorrect type is undefined behavior.
Source§impl<A> Box<dyn Any + Sync + Send, A>where
A: Allocator,
impl<A> Box<dyn Any + Sync + Send, A>where
A: Allocator,
1.51.0 · Sourcepub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Sync + Send, A>>where
T: Any,
pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Sync + Send, A>>where
T: Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where
T: Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative see downcast.
§Examples
#![feature(downcast_unchecked)]
use std::any::Any;
let x: Box<dyn Any + Send + Sync> = Box::new(1_usize);
unsafe {
assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}§Safety
The contained value must be of type T. Calling this method
with the incorrect type is undefined behavior.
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".