6 releases

Uses old Rust 2015

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

#259 in Rust patterns

Download history 18043/week @ 2023-12-15 15025/week @ 2023-12-22 13845/week @ 2023-12-29 18006/week @ 2024-01-05 20252/week @ 2024-01-12 20388/week @ 2024-01-19 22288/week @ 2024-01-26 25608/week @ 2024-02-02 25280/week @ 2024-02-09 25875/week @ 2024-02-16 25192/week @ 2024-02-23 25428/week @ 2024-03-01 27282/week @ 2024-03-08 26222/week @ 2024-03-15 26349/week @ 2024-03-22 22254/week @ 2024-03-29

106,063 downloads per month
Used in 105 crates (16 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