pub trait ExtAny: Any + Sealed {
// Provided methods
fn type_id() -> TypeId { ... }
fn type_of(&self) -> TypeId { ... }
fn type_name(&self) -> &'static str ⓘ { ... }
fn type_is<T: 'static>(&self) -> bool { ... }
fn as_any_ref(&self) -> &dyn Any
where Self: Sized { ... }
fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized { ... }
fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized { ... }
fn downcast_ref<T: 'static>(&self) -> Option<&T> ⓘ { ... }
fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> ⓘ { ... }
}
Expand description
Extension trait providing convenience methods for T:
Any
.
This trait is sealed and cannot be implemented manually.
Provided Methods§
Sourcefn type_id() -> TypeId
fn type_id() -> TypeId
Returns the TypeId
of Self
.
§Example
use devela::ExtAny;
let x = 5;
assert_eq!(x.type_of(), i32::type_id());
Sourcefn type_of(&self) -> TypeId
fn type_of(&self) -> TypeId
Returns the TypeId
of self
.
§Example
use devela::ExtAny;
let x = 5;
assert_eq!(x.type_of(), i32::type_id());
Sourcefn type_name(&self) -> &'static str ⓘ
fn type_name(&self) -> &'static str ⓘ
Returns the type name of self
.
§Example
use devela::code::ExtAny;
let x = 5;
assert_eq!(x.type_name(), "i32");
Sourcefn type_is<T: 'static>(&self) -> bool
fn type_is<T: 'static>(&self) -> bool
Returns true
if Self
is of type T
.
§Example
use devela::ExtAny;
let val = 5;
assert!(val.type_is::<i32>());
assert!(!val.type_is::<u32>());
// Compared to Any::is():
let any = val.as_any_ref();
// assert!(any.type_is::<i32>()); // doesn't work for &dyn Any
// assert!(val.is::<i32>()); // doesn't work for T: Any
assert!(any.is::<i32>()); // does work for &dyn Any
Sourcefn as_any_ref(&self) -> &dyn Anywhere
Self: Sized,
fn as_any_ref(&self) -> &dyn Anywhere
Self: Sized,
Upcasts &self
as &dyn Any
.
§Example
use devela::{Any, ExtAny};
let val = 5;
let any: &dyn Any = &val as &dyn Any;
assert!(any.is::<i32>()); // works direcly for dyn Any
Sourcefn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Upcasts &mut self
as &mut dyn Any
.
§Example
use devela::{Any, ExtAny};
let mut x = 5;
let any: &mut dyn Any = x.as_any_mut();
assert!(any.is::<i32>());
Sourcefn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
Upcasts Box<self>
as Box<dyn Any>
.
§Example
use devela::{Any, ExtAny};
let x = Box::new(5);
let any: Box<dyn Any> = x.as_any_box();
assert!(any.is::<i32>());
Sourcefn downcast_ref<T: 'static>(&self) -> Option<&T> ⓘ
Available on crate feature unsafe_layout
only.
fn downcast_ref<T: 'static>(&self) -> Option<&T> ⓘ
unsafe_layout
only.Returns some shared reference to the inner value if it is of type T
.
This method is only needed when not dealing directly with dyn Any
trait objects,
since it’s already implemented for dyn Any
.
§Example
use core::fmt::Display;
use devela::{Any, ExtAny};
trait Trait: Any + Display {}
impl Trait for i32 {}
impl Trait for char {}
impl Trait for bool {}
// in the heap:
{
let b: Box<dyn Trait> = Box::new(5);
if let Some(n) = (*b).downcast_ref::<i32>() {
assert_eq![n, &5_i32];
}
let bb: Vec<Box<dyn Trait>> = vec![Box::new(true), Box::new(6), Box::new('c')];
for b in bb {
if let Some(n) = (*b).downcast_ref::<i32>() {
assert_eq![n, &6_i32];
}
}
}
// in the stack:
{
use devela::{Any, DstArray, DstStack, DstValue, ExtAny};
let v = DstValue::<dyn Trait, DstArray<usize, 2>>::new(7, |v| v as _).unwrap();
if let Some(n) = (*v).downcast_ref::<i32>() {
assert_eq![n, &7_i32];
}
let mut vv = DstStack::<dyn Trait, DstArray<u32, 12>>::new();
vv.push(true, |v| v).unwrap();
vv.push(8_i32, |v| v).unwrap();
vv.push('c', |v| v).unwrap();
for v in vv.iter() {
if let Some(n) = (*v).downcast_ref::<i32>() {
assert_eq![n, &8_i32];
}
}
}
Sourcefn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> ⓘ
Available on crate feature unsafe_layout
only.
fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> ⓘ
unsafe_layout
only.Returns some exclusive reference to the inner value if it is of type T
.
This method is only needed when not dealing directly with dyn Any
trait objects,
since it’s already implemented for dyn Any
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.