I have a situation similar to the following:
use std::rc::Rc;
trait A {}
trait B: A {}
struct Impl {}
impl A for Impl {}
impl B for Impl {}
I would like to be able to cast trait objects of these traits like in the following:
fn main() {
let val: Rc<Box<A>> = Rc::new(Box::new(Impl {}));
let test: Rc<Box<B>> = val as Rc<Box<B>>;
}
This gives me a compiler-error that this is a non-scalar cast. I guess I am doing the wrong sort of cast, but I do not see how this is supported. Further, I would like the cast to be safe, i.e. returning me an Option<Rc<Box<B>>
. Is this possible or is the dynamic dispatch implemented such that trait objects cannot be converted between traits?
Edit: I forgot to say, I am using the nightly version, in case this should matter.