I encountered this issue, and I'm wondering why it doesn't work. I managed to replicate the problem :
I'm trying to get back to the struct from &mut Box<dyn Trait>
, and this is my go at it:
use std::any::Any;
trait A {}
struct B;
impl A for B {}
fn main() {
let mut a: Box<dyn A> = Box::new(B);
let a_ref: &mut Box<dyn A> = &mut a;
match (a_ref as &mut dyn Any).downcast_mut::<B>() { // downcast here
Some(_b) => println!("found b"),
None => println!("found nothing")
}
}
But this does not find the struct behind it ? Am I missing something ?
Here is a link to the playground
I've tried to downcast to other things, such as Box<B>
or &mut B
but none if this works.