AnyTrait

Trait AnyTrait 

Source
pub trait AnyTrait: 'static {
    // Required methods
    fn type_ids(&self) -> &'static [TypeIdConst];
    fn type_erase(&self, trait_num: usize) -> AnyPtr;
    fn type_erase_mut(&mut self, trait_num: usize) -> AnyPtr;
}
Expand description

§AnyTrait

Don’t implement manually

use #[derive(AnySubTrait)]


Imagine a Concrete type and all its subtraits
AnyTrait lets you walk up and down the traits safely

With ::core::any::Any you can only cast between the concrete type and Any.
With AnyTrait you can do that, plus any other trait in the middle

AnyTrait is not necessarily fast as it needs check and track the list of traits you are allowed to cast to.

Required Methods§

Source

fn type_ids(&self) -> &'static [TypeIdConst]

returns a list of all possible traits that you can up/downcast to
This list always has at least two elements:

  • id 0: TypeIdConst::of::<dyn AnyType>
  • id 1: TypeIdConst::of::<YourConcreteType>

The reset of the list is currently unordered, will change as soon as we find a way to have a const Ord on TypeId

Source

fn type_erase(&self, trait_num: usize) -> AnyPtr

don’t use. internal only.

cast self to a trait in the .type_ids() list.
the pointer to the ref to the type in the list is then type-erase to AnyPtr.

§Safety

This is safe since you can’t do anything to a AnyPtr by itself. AnyPtr::to_ptr however is just wrong if you don’t have the right type. Again, don’t use: internal only

§Panics

If list trait_num exceeds type_ids() length

Source

fn type_erase_mut(&mut self, trait_num: usize) -> AnyPtr

don’t use. internal only.

cast self to a trait in the .type_ids() list.
the pointer to the ref to the type in the list is then type-erase to AnyPtr.

§Safety

This is safe since you can’t do anything to a AnyPtr by itself AnyPtr::to_ptr however is just wrong if you don’t have the right type. Again, don’t use: internal only

§Panics

If list trait_num exceeds type_ids() length

Implementors§