|
|
|
|
|
by cousin_it
3329 days ago
|
|
How about this? struct Foo { bar: Bar }
struct Bar { message: &'static str }
fn change_foo(foo: &mut Foo) {
change_bar(&mut foo.bar);
}
fn change_bar(bar: &mut Bar) {
bar.message = "Invalid";
if true {
panic!();
}
bar.message = "Valid";
}
impl Drop for Foo {
fn drop(&mut self) {
println!("{}", self.bar.message);
}
}
fn main() {
let mut foo = Foo { bar: Bar { message: "" } };
change_foo(&mut foo);
}
The destructor of a struct sees a broken invariant of a nested struct. |
|
For what it's worth, the solution I've seen for this type of case is another struct that is used to restore to an acceptable state: