Crate bombs

source · []
Expand description

Bombs, a whole load of ’em.

This crate provides single-producer multi-consumer communication types, where Fuses are the producers and Bombs are the consumers.

Bombs are one-time use only, whereas MultiBombs can be reused.

Example

If you want to send a “close program” signal to different threads, such that each thread needs to clean up first before closing, you can give each thread a Bomb<()>, and run cleanup code once exploded.

use std::thread;
use bombs::Bomb;
 
// Create a new fuse and bomb pair.
let (fuse, bomb) = Bomb::new(); 
 
// Clone `bomb` into thread.
let bomb_clone = bomb.clone();
thread::spawn(move || {
    loop {
        // Do some stuff...
     
        if let Some(_) = bomb_clone.exploded() {
            // Received close signal, break.
 
            // Clean up data values...
 
            break;
        }
    }
});
 
// Create another thread.
// Move original `bomb` into thread.
thread::spawn(move || {
    loop {
        // Do some other stuff...
     
        if let Some(_) = bomb.exploded() {
            // Received close signal, break.
 
            // Clean up data values...
 
            break;
        }
    }
});
 
// Do some different stuff...
 
// Send close signal.
let fire = fuse.light(());
 
// Wait for all inner threads to close safely (checked by `Bomb` drop).
while !fire.extinguished() { }
 
// Now safely quit the program.

Structs

A one-time use bomb.

Result of a bomb explosion.

A one-time use fuse.

A reuseable bomb. How did they manufacture this?

A reusable fuse. It just grows back.

Type Definitions

A type alias for Fuse<T>.

A type alias for MultiFuse<T>.