any-trait 0.1.0

Up/Down cast between concrete and subtraits
Documentation

AnyTrait

AnyTrait lets you upcast to a generic &dyn AnyTrait like [::core::any::Any]
but instead of just allowing you to downcast back to the concrete type, it also lets you downcast to any trait used by your type

This is not zero-cost, since at any cast we need to go through the list of all possible subtraits.

This will (almost) enable you to do OOP in rust, but if this is your goal we still ask you to kindly reconsider

example usage:

use any_trait::{AnySubTrait, AnyTrait, AsAnyTrait};
trait TA {}
trait TB : AnyTrait {} // if a trait implements `AnyTrait` you can upcast
#[derive(AnySubTrait)]
#[any_sub_trait(TA, TB)] // must include all traits you want to downcast to
struct Concrete {
    // whatever
}
impl TA for Concrete {}
impl TB for Concrete {}
fn test() {
    let c = Concrete{};
    let a = c.as_anytrait();

    let ta :&dyn TA = a.downcast_ref::<dyn TA>().unwrap();
    let tb :&dyn TB = a.downcast_ref::<dyn TB>().unwrap();

    let a2 = tb.as_anytrait();
    let c_ref : &Concrete = a2.downcast_ref::<Concrete>().unwrap();
}