#![cfg_attr(feature = "min_specialization", feature(min_specialization))] #![cfg_attr(feature = "const_sort", feature(const_trait_impl, const_mut_refs))] use std::{any::type_name, fmt::Display}; use trait_cast_rs::{make_trait_castable_decl, TraitcastableAny, TraitcastableAnyInfra}; struct HybridPet { name: T, } impl HybridPet { fn greet(&self) { println!("{}: Hi {}", self.name, type_name::()) } } impl Dog for HybridPet { fn bark(&self) { println!("{}: Woof!", self.name); } } impl Cat for HybridPet { fn meow(&self, speak: &V) { println!("{}: Meow! {speak}", self.name); } } trait Dog { fn bark(&self); } /// Note: The `+ ?Sized` trait bound is not generally required but used to allow `str`. trait Cat { fn meow(&self, speak: &T); } // With the decl_macro you can't (yet) be generic over a T, so we only make `HybridPet` && `HybridPet` traitcastable. make_trait_castable_decl! { HybridPet => (Dog, Cat), HybridPet => (Dog, Cat), } #[cfg_attr(test, test)] fn main() { // The box is technically not needed but kept for added realism let pet = Box::new(HybridPet { name: "Kokusnuss".to_string(), }); pet.greet(); let castable_pet: Box = pet; let as_dog: &dyn Dog = castable_pet.downcast_ref().unwrap(); as_dog.bark(); let as_cat: &dyn Cat = castable_pet.downcast_ref().unwrap(); as_cat.meow("Text"); let cast_back: &HybridPet = castable_pet.downcast_ref().unwrap(); cast_back.greet(); }