6 releases

Uses old Rust 2015

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

#396 in Rust patterns

Download history 28750/week @ 2024-10-10 29397/week @ 2024-10-17 31350/week @ 2024-10-24 32149/week @ 2024-10-31 31907/week @ 2024-11-07 32440/week @ 2024-11-14 32878/week @ 2024-11-21 33794/week @ 2024-11-28 36222/week @ 2024-12-05 33193/week @ 2024-12-12 25394/week @ 2024-12-19 21569/week @ 2024-12-26 31109/week @ 2025-01-02 31642/week @ 2025-01-09 33162/week @ 2025-01-16 23256/week @ 2025-01-23

122,721 downloads per month
Used in 124 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