I am currently trying to understand how it works drop. The following code fails, and I do not understand why. In my opinion, use std::ptr::writeshould prevent the destructor from starting (edit: original value here: Rc) (in this case, nothing should happen next to a memory leak). But it doesn't seem to be that way ( playpen , compile with -O0)
use std::rc::Rc;
use std::mem;
use std::ptr;
enum Foo {
Bar(Rc<usize>),
Baz
}
use Foo::*;
impl Drop for Foo {
fn drop(&mut self) {
match *self {
Bar(_) => {
unsafe { ptr::write(self, Foo::Baz) }
}
Baz => ()
}
}
}
fn main() {
let _ = Foo::Bar(Rc::new(23));
}
gives an overflow error:
thread '<main>' panicked at 'arithmetic operation overflowed', /Users/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-mac/build/src/liballoc/rc.rs:755
Another option terminates with illegal instruction. Why is this happening? How can I replace self with a value that will be correctly omitted?
Ferio source
share