Skip to main content

AnyExt

Trait AnyExt 

Source
pub trait AnyExt: 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 type_hash(&self) -> u64 { ... }
    fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64 { ... }
    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.


📍 code


This trait is sealed and cannot be implemented manually.

Provided Methods§

Source

fn type_id() -> TypeId

Returns the TypeId of Self.

§Examples
use devela::AnyExt;

let x = 5;
assert_eq!(x.type_of(), i32::type_id());
Source

fn type_of(&self) -> TypeId

Returns the TypeId of self.

§Examples
use devela::AnyExt;

let x = 5;
assert_eq!(x.type_of(), i32::type_id());
Source

fn type_name(&self) -> &'static str

Returns the type name of self.

§Examples
use devela::AnyExt;

let x = 5;
assert_eq!(x.type_name(), "i32");

See also any_type_name.

Source

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T.

§Examples
use devela::AnyExt;

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
Source

fn type_hash(&self) -> u64

Returns a deterministic hash of the TypeId of Self.

Source

fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64

Returns a deterministic hash of the TypeId of Self using a custom hasher.

Source

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any.

§Examples
use devela::{Any, AnyExt};

let val = 5;
let any: &dyn Any = &val as &dyn Any;
assert!(any.is::<i32>()); // works direcly for dyn Any
Source

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any.

§Examples
use devela::{Any, AnyExt};

let mut x = 5;
let any: &mut dyn Any = x.as_any_mut();
assert!(any.is::<i32>());
Source

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Available on crate feature alloc only.

Upcasts Box<self> as Box<dyn Any>.

§Examples
use devela::{Any, AnyExt};

let x = Box::new(5);
let any: Box<dyn Any> = x.as_any_box();
assert!(any.is::<i32>());
Source

fn downcast_ref<T: 'static>(&self) -> Option<&T>

Available on crate feature unsafe_layout and non-crate feature safe_code 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.

§Examples
use core::fmt::Display;
use devela::{Any, AnyExt};

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, AnyExt};
    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];
        }
    }
}
Source

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T>

Available on crate feature unsafe_layout and non-crate feature safe_code 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".

Implementors§

Source§

impl<T: ?Sized + Any> AnyExt for T