pub trait Any: 'static { // Required method fntype_id(&self) ->TypeId;}Expand description
A trait to emulate dynamic typing.
Most types implementAny. However, any type which contains a non-'static reference does not.See themodule-level documentation for more details.
Required Methods§
Implementations§
Source§impl dynAny
impl dynAny
1.0.0 ·Sourcepub fnis<T>(&self) ->boolwhere T:Any,
pub fnis<T>(&self) ->boolwhere T:Any,
Returnstrue if the inner type is the same asT.
§Examples
1.0.0 ·Sourcepub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
pub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
Returns some reference to the inner value if it is of typeT, orNone if it isn’t.
§Examples
1.0.0 ·Sourcepub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
pub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
Returns some mutable reference to the inner value if it is of typeT, orNone if it isn’t.
§Examples
Sourcepub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
downcast_unchecked #90850)Returns a reference to the inner value as typedyn T.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.
Sourcepub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
downcast_unchecked #90850)Returns a mutable reference to the inner value as typedyn T.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = 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 typeT. Calling this methodwith the incorrect type isundefined behavior.
Source§impl dynAny +Send
impl dynAny +Send
1.0.0 ·Sourcepub fnis<T>(&self) ->boolwhere T:Any,
pub fnis<T>(&self) ->boolwhere T:Any,
Forwards to the method defined on the typedyn Any.
§Examples
1.0.0 ·Sourcepub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
pub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
Forwards to the method defined on the typedyn Any.
§Examples
1.0.0 ·Sourcepub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
pub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
Forwards to the method defined on the typedyn Any.
§Examples
Sourcepub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
downcast_unchecked #90850)Forwards to the method defined on the typedyn Any.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.
Sourcepub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
downcast_unchecked #90850)Forwards to the method defined on the typedyn Any.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = 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 typeT. Calling this methodwith the incorrect type isundefined behavior.
Source§impl dynAny +Send +Sync
impl dynAny +Send +Sync
1.28.0 ·Sourcepub fnis<T>(&self) ->boolwhere T:Any,
pub fnis<T>(&self) ->boolwhere T:Any,
Forwards to the method defined on the typeAny.
§Examples
1.28.0 ·Sourcepub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
pub fndowncast_ref<T>(&self) ->Option<&T>where T:Any,
Forwards to the method defined on the typeAny.
§Examples
1.28.0 ·Sourcepub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
pub fndowncast_mut<T>(&mut self) ->Option<&mut T>where T:Any,
Forwards to the method defined on the typeAny.
§Examples
Sourcepub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_ref<T>(&self) ->&Twhere T:Any,
downcast_unchecked #90850)Forwards to the method defined on the typeAny.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.
Sourcepub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut Twhere T:Any,
downcast_unchecked #90850)Forwards to the method defined on the typeAny.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = 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 typeT. Calling this methodwith the incorrect type isundefined behavior.
Source§impl<A>Box<dynAny, A>where A:Allocator,
impl<A>Box<dynAny, A>where A:Allocator,
1.0.0 ·Sourcepub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny, A>>where T:Any,
pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny, A>>where T:Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative seedowncast.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.
Source§impl<A>Box<dynAny +Send, A>where A:Allocator,
impl<A>Box<dynAny +Send, A>where A:Allocator,
1.0.0 ·Sourcepub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send, A>>where T:Any,
pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send, A>>where T:Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative seedowncast.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny + Send> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.
Source§impl<A>Box<dynAny +Send +Sync, A>where A:Allocator,
impl<A>Box<dynAny +Send +Sync, A>where A:Allocator,
1.51.0 ·Sourcepub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send +Sync, A>>where T:Any,
pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send +Sync, A>>where T:Any,
Attempts to downcast the box to a concrete type.
§Examples
Sourcepub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
🔬This is a nightly-only experimental API. (downcast_unchecked #90850)
pub unsafe fndowncast_unchecked<T>(self) ->Box<T, A>where T:Any,
downcast_unchecked #90850)Downcasts the box to a concrete type.
For a safe alternative seedowncast.
§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny + Send + Sync> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}§Safety
The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.