6 releases

Uses old Rust 2015

0.1.6 Sep 25, 2021
0.1.5 Jul 31, 2020
0.1.4 Aug 1, 2018

#181 in Rust patterns

Download history 13993/week @ 2022-11-28 16366/week @ 2022-12-05 13494/week @ 2022-12-12 14717/week @ 2022-12-19 12609/week @ 2022-12-26 13687/week @ 2023-01-02 11570/week @ 2023-01-09 15871/week @ 2023-01-16 14452/week @ 2023-01-23 13845/week @ 2023-01-30 13557/week @ 2023-02-06 17397/week @ 2023-02-13 14774/week @ 2023-02-20 18090/week @ 2023-02-27 15245/week @ 2023-03-06 16122/week @ 2023-03-13

65,715 downloads per month
Used in 69 crates (9 directly)

MIT/Apache

7KB
142 lines

drop_bomb

Build Status Crates.io API reference

A runtime guard for (protecting your precious bodily fluids) implementing linear types. See the docs for more.


lib.rs:

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::{Write, BufWriter, Result};
use drop_bomb::DropBomb;

struct CheckedBufWriter<W: Write> {
    inner: BufWriter<W>,
    bomb: DropBomb,
}

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

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

Notes:

  • Bombs do nothing if a thread is already panicking.
  • When #[cfg(debug_assertions)] is disabled, DebugDropBomb is always defused and has a zero size.

No runtime deps