struct Inherited;
struct Direct;
use std::any::Any;
trait Tr : Any {}
impl Tr for Inherited {}
fn main() {
let a: Box<Any> = Box::new(Direct);
let b: Box<Tr> = Box::new(Inherited);
let a: &Direct = a.downcast_ref().unwrap();
// Does not compile:
// let b: &Inherited = b.downcast_ref().expect("that should work too");
let b: &Inherited = (&b as &Any).downcast_ref().expect("that should work too");
}
Since the trait implements Any
, I'd expect it to support Any
's methods. But somehow it doesn't, and casting Box
to reference seems to change how the type is interpreted, because the cast fails. What's going on here?