Here is some example code:
struct Bar {
x: f32
}
struct Buz {
data: [f32; 16]
}
struct Foo {
foobar: Bar,
foobuz: Buz
}
impl Foo {
pub fn get_bar_mut(&mut self) -> &mut Bar {
&mut self.foobar
}
pub fn get_buz(&self) -> &Buz {
&self.foobuz
}
}
fn do_stuff(foo: &mut Foo) {
let buzzy = foo.get_buz();
let barry = foo.get_bar_mut();
if buzzy.data[0] == 0.0 {
barry.x = 2.0;
}
}
This code won't compile and I understand why. Given this context, I believe the code inside do_stuff
and, in general, any code in patterned like that, is safe. Is there anyway to do something similar to what is done in do_stuff
, or am I completely wrong and this code is indeed unsafe?