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
sourceimpl<A> Box<dyn Any + 'static, A>where
A: Allocator,
impl<A> Box<dyn Any + 'static, A>where
A: Allocator,
sourcepub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + 'static, A>>where
T: Any,
pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + 'static, A>>where
T: Any,
Attempt to downcast the box to a concrete type.
Examples
use std::any::Any;
fn print_if_string(value: Box<dyn Any>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}
let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Runsourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
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);
}
RunSafety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
sourceimpl<A> Box<dyn Any + Send + 'static, A>where
A: Allocator,
impl<A> Box<dyn Any + Send + 'static, A>where
A: Allocator,
sourcepub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Send + 'static, A>>where
T: Any,
pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Send + 'static, A>>where
T: Any,
Attempt to downcast the box to a concrete type.
Examples
use std::any::Any;
fn print_if_string(value: Box<dyn Any + Send>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}
let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Runsourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
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);
}
RunSafety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
sourceimpl<A> Box<dyn Any + Send + Sync + 'static, A>where
A: Allocator,
impl<A> Box<dyn Any + Send + Sync + 'static, A>where
A: Allocator,
1.51.0 · sourcepub fn downcast<T>(
self
) -> Result<Box<T, A>, Box<dyn Any + Send + Sync + 'static, A>>where
T: Any,
pub fn downcast<T>(
self
) -> Result<Box<T, A>, Box<dyn Any + Send + Sync + 'static, A>>where
T: Any,
Attempt to downcast the box to a concrete type.
Examples
use std::any::Any;
fn print_if_string(value: Box<dyn Any + Send + Sync>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}
let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Runsourcepub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
where
T: Any,
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
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);
}
RunSafety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
sourceimpl dyn Any + 'static
impl dyn Any + 'static
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
use std::any::Any;
fn print_if_string(s: &dyn Any) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Runsourcepub 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
use std::any::Any;
fn modify_if_u32(s: &mut dyn Any) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<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_ref_unchecked::<usize>(), 1);
}
RunSafety
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>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<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_mut_unchecked::<usize>() += 1;
}
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
RunSafety
The contained value must be of type T
. Calling this method
with the incorrect type is undefined behavior.
sourceimpl dyn Any + Send + 'static
impl dyn Any + Send + 'static
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
use std::any::Any;
fn print_if_string(s: &(dyn Any + Send)) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Runsourcepub 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
use std::any::Any;
fn modify_if_u32(s: &mut (dyn Any + Send)) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere
T: Any,
downcast_unchecked
#90850)sourcepub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<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_mut_unchecked::<usize>() += 1;
}
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
RunSafety
Same as the method on the type dyn Any
.
sourceimpl dyn Any + Send + Sync + 'static
impl dyn Any + Send + Sync + 'static
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
use std::any::Any;
fn print_if_string(s: &(dyn Any + Send + Sync)) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Run1.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
use std::any::Any;
fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere
T: Any,
downcast_unchecked
#90850)sourcepub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere
T: Any,
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere
T: Any,
downcast_unchecked
#90850)