#type #linear #drop #run-time #panic #bomb #linear-types

drop_bomb

A runtime guard for implementing linear types

6 releases

Uses old Rust 2015

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

#247 in Rust patterns

Download history 17546/week @ 2023-11-20 17970/week @ 2023-11-27 18561/week @ 2023-12-04 17906/week @ 2023-12-11 18676/week @ 2023-12-18 12896/week @ 2023-12-25 15358/week @ 2024-01-01 19331/week @ 2024-01-08 20118/week @ 2024-01-15 19565/week @ 2024-01-22 24264/week @ 2024-01-29 26435/week @ 2024-02-05 25423/week @ 2024-02-12 24763/week @ 2024-02-19 25798/week @ 2024-02-26 26295/week @ 2024-03-04

104,985 downloads per month
Used in 106 crates (17 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