Trait devela::code::ExtAny

source ·
pub trait ExtAny: Any + Sealed {
    // Provided methods
    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 { ... }
}
Expand description

Extension trait providing convenience methods for T: Any.

This trait is sealed and cannot be implemented manually.

Provided Methods§

source

fn type_of(&self) -> TypeId

Returns the TypeId of self.

§Examples
use devela::code::{ExtAny, TypeId};

 let x = 5;
 assert_eq!(x.type_of(), TypeId::of::<i32>());
source

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

Returns the type name of self.

§Examples
use devela::code::ExtAny;

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

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

Returns true if Self is of type T.

§Examples
use devela::code::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
source

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

Upcasts &self as &dyn Any.

§Examples
use devela::code::{Any, ExtAny};

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::code::{Any, ExtAny};

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,

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

§Examples
use devela::code::{Any, ExtAny};

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

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Any> ExtAny for T