Can I have a mutable reference to a value and a mutable reference to a trait object of the same value inside the same scope? Is that undefined behavior? An example code snippet is added below for clarification.
In the below code is it valid to have the check and check_trait references at the same time.
pub trait fire {
fn hi(&self);
}
struct foo {
bar: isize,
}
impl foo {
fn new(x: isize) -> Self {
foo { bar: x }
}
}
impl fire for foo {
fn hi(&self) {
println!("hi");
}
}
fn main() {
let mut init = Box::new(foo::new(1));
let leaked = Box::into_raw(x);
let check = unsafe { leaked.as_mut().unwrap() };
let mut trait_object:Box<dyn fire> = unsafe {
Box::from_raw(leaked)
};
let check_trait = &mut trait_object;
println!("{}", check.bar);
check_trait.hi(5);
}