fn get_caster(&self, tp: TypeId) -> Option<&'static dyn Any>;
}
impl_downcast!(sync HasPseudoMethods);
-/*
-pub trait HasPseudoMethodsExt {
- fn cast_to<T: 'static>(&self) -> Option<Box<T>>;
-}
-
-impl<T:HasPseudoMethods+?Sized> HasPseudoMethodsExt for T {
- fn cast_to<U: 'static>(&self) -> Option<Box<U>> {
- let instance = self.cast_to_instance(TypeId::of::<U>())?;
- instance.downcast().ok()
- }
-}*/
trait HasFeet {
fn how_many_feet(&self) -> usize;
}
-trait EatsFood {
- fn favorite_food(&self) -> &'static str;
-}
-
struct Frog;
impl HasFeet for Frog {
fn how_many_feet(&self) -> usize { 4 }
}
-/*
-impl EatsFood for Frog {
- fn favorite_food(&self) -> &'static str {
- "flies"
- }
-}*/
-
impl HasPseudoMethods for Frog {
// Note: if you wanted to be able to add more traits to Frog elsewhere, you would
// need a registry of cast-to-trait objects, similar to the existing dispatch
if tp == TypeId::of::<
&'static dyn HasFeet
>() {
-/* let arc: Arc<dyn Any + Send + Sync> = DowncastSync::into_any_arc(arc.clone());
- let arc: Arc<Frog> = Arc::downcast::<Frog>(arc).unwrap();
- let arc: Arc<dyn HasFeet> = arc as _;
- */
static CASTER: fn(&dyn HasPseudoMethods) -> &dyn HasFeet
= |self_| {
let self_: &dyn Any = Downcast::as_any(self_);
};
return Some(&CASTER);
-/* } else if tp == TypeId::of::<Arc<dyn EatsFood>>() {
- let arc: Arc<dyn EatsFood> = self.clone();
- return Some(Box::new(arc));
-*/
}
None
}