Crate drop_bomb[][src]

drop_bomb

drop_bomb provides two types, DropBomb and DebugDropBomb, which panic in drop with a specified message unless defused. This is useful as a building-block for runtime-checked linear types.

For example, one can build a variant of BufWriter which enforces handling of errors during flush.

extern crate drop_bomb;

use std::io;
use drop_bomb::DropBomb;

struct CheckedBufWriter<R> {
    inner: R,
    bomb: DropBomb,
}

impl<R: io::Write> CheckedBufWriter<R> {
    fn new(inner: R) -> CheckedBufWriter<R> {
        let bomb = DropBomb::new(
            "CheckedBufWriter must be explicitly closed \
             to handle potential errors on flush"
        );
        CheckedBufWriter { inner, bomb }
    }

    fn close(mut self) -> io::Result<()> {
        self.bomb.defuse();
        self.inner.flush()?;
        Ok(())
    }
}

Notes:

* Bombs do nothing if a thread is already panicking.
* When `cfg(debug_assertions)` is enabled, `DebugDropBomb` is
  an always defused and has a zero size.

Structs

DebugDropBomb
DropBomb