Hello Everyone,
I'm trying to use exactly the same pattern that the Any
trait uses for downcast_ref
: i.e.
pub trait QWSElement {
fn payload<T:Sized>(&self) -> Option<&T>;
}
But the generic is preventing it from being made into an object.
element : Box<dyn QWSElement>
| QWSElement` cannot be made into an object
...because method `payload` has generic type parameters
Anyway there is a work-around, where I can cast my &dyn QWSElement
to an &dyn Any
, and then call Any
's downcast_ref
, but I'd like to expose a cleaner interface.
e.g.
element.payload::<i32>().unwrap()
instead of:
element.as_any().downcast_ref::<QWSElementWrapper<i32>>().unwrap().payload()
How does the Any
trait make this work? Does the compiler just know to treat it as special, or is there some mechanism to allow generic types to work with trait objects in the general case?
Thank you.