I have a trait and a struct that implements that trait:
trait A {
fn something(&self);
}
struct B {
some_field: i32,
}
impl A for B {
fn something(&self) {}
}
There is a part of my code where I have an &A
, and I know it is an instance of B
, I would like to cast &A
to &B
. How can I accomplish this? So far, I have tried first casting to *const A
and then casting to *const B
, but I can't figure out how to go from *const B
to &B
.
After playing around with some things, I think this is correct?
fn some_func(a: &dyn A) {
let a_ptr = a as *const A;
let b_ptr = a_ptr as *const B;
unsafe { let b = &*b_ptr as &B; }
}