I’m trying to do this:
use std::any::Any;
trait A: Any + 'static {}
#[derive(Debug)]
struct S {
i: i64,
}
impl A for S {}
fn main() {
let a: Box<dyn Any> = Box::new(S { i: 1 });
let b: Box<dyn A> = Box::new(S { i: 2 });
let c: Box<S> = Box::new(S { i: 3 });
let d = S { i: 4 };
let e = &d;
println!(
"{:?} {:?}",
(*a).downcast_ref::<S>(), // works
(*b).downcast_ref::<S>(), // does not work
);
}
I get the following compilation error:
error[E0599]: no method named `downcast_ref` found for trait object `(dyn A + 'static)` in the current scope
--> src/main.rs:18:14
|
18 | (*b).downcast_ref::<S>(),
| ^^^^^^^^^^^^ method not found in `dyn A`
Which is strange, because A
explicitly requires Any
. First I tried without being explicit about it, but it didn’t work that way either.
I also tried this:
(&*b as &dyn Any).downcast_ref::<S>()
But in this case I get the following error:
error[E0658]: cannot cast `dyn A` to `dyn Any`, trait upcasting coercion is experimental
--> src/main.rs:19:10
|
19 | (&*b as &dyn Any).downcast_ref::<S>(),
| ^^^
|
Box
has a downcast()
method, but it eats the box, while I want to work on a reference. My original use case has b
as a &Box<dyn A>
, in which case the above code compiles but doesn’t work because it converts the Box
to Any
instead of the underlying value. The equivalent code, &**b as &dyn Any
, gives the same compile error.
Is there any way to downcast a &Box<dyn A>
to a &S
?
3