src/plop3.rs
struct Bomb;
impl Drop for Bomb {
fn drop(&mut self) {
unsafe extern "C" {
#[link_name = "\n\nerror: `mandatory_method()` was not called before drop\n\n"]
fn mandatory_method_not_called();
}
unsafe { mandatory_method_not_called() }
}
}
pub struct PhantomNoDrop {
bomb: Bomb,
}
impl PhantomNoDrop {
pub fn new() -> Self {
Self { bomb: Bomb }
}
pub fn mandatory_method(self) {
core::mem::forget(self.bomb);
}
}
struct Undroppable {
p: PhantomNoDrop,
}
impl Undroppable {
pub fn mandatory_method(self) {
self.p.mandatory_method();
}
}
fn main() {
let p = PhantomNoDrop::new();
p.mandatory_method();
let u = Undroppable {
p: PhantomNoDrop::new(),
};
}