Suppose we have:
pub struct Cat {}
pub struct Dog {}
pub trait Animal_T {}
impl Animal_T for Cat {}
impl Animal_T for Dog {}
is there a safe way to do
&dyn Animal_T -> Option<&Cat> or
Rc<dyn Animal_T> -> Option<Rc<Cat>> or
... something like this ...
Thanks!
Not directly. Typically, this is done via one of two mechanisms:
- Using
Any, by adding something like an Animal_T::as_any method.
- Implementing manual
Animal_T::as_cat etc. methods.
Not using this method (long story, involves compilation time / crate dependencies.)
Is the idea here Animal_T -> Any -> downcast to Option<Cat> ?