Summary
I wanted to write a into_trait
to cast a smart pointer of type Rc<ConcreteType>
into Rc<dyn Trait>
fn into_trait<T, U>(concrete: Rc<T>) -> Rc<dyn U> where T: U;
The above function won't compile. Is there any way to do this?
Context
I was writing a wrapper for Rc
. I noticed that Rc<ConcreteType>
can be converted into Rc<dyn Trait>
. As far as I understood, this could be done because Rc
implemented CoerceUnsized
, which was unstable now. I was trying to write a function (rather than a macro) to convert Wrapper<Concrete>
into Wrapper<dyn Trait>
.
Code
The following code was also put in the playground.
use std::{rc::Rc, cell::RefCell, any::Any};
struct Wrapper<T: ?Sized + 'static> {
inner: Rc<T>
}
impl<T> Wrapper<T> {
// this is fine
fn into_any(self) -> Wrapper<dyn Any> {
Wrapper { inner: self.inner }
}
// -----> how can I achieve this? <-----
// It's something can be done with std::is_base_of in C++,
// but I don't know how to do it in Rust
fn into_trait<U>(self) -> Wrapper<dyn U> where T: U {
Wrapper { inner: self.inner }
}
}
fn main() {
let w = Wrapper { inner: Rc::new(RefCell::new(1)) };
let w = w.into_trait::<Any>(); // <---- would be called here
}