Crate killswitch[][src]

Signal a request for (multiple) async tasks to self-terminate.

use std::error::Error;
use tokio::time::{sleep, Duration};
use killswitch::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
  let (killswitch, shutdown) = killswitch();

 tokio::spawn(croakable(String::from("test1"), shutdown.clone()));
 tokio::spawn(croakable(String::from("test2"), shutdown.clone()));

 sleep(Duration::from_secs(1)).await;

 println!("Trigging kill switch");
 killswitch.trigger();

 tokio::spawn(croakable(String::from("test3"), shutdown.clone()));
 tokio::spawn(croakable(String::from("test4"), shutdown));

 sleep(Duration::from_secs(1)).await;

 Ok(())
}

async fn croakable(s: String, shutdown: Shutdown) {
 println!("croakable({}) entered", s);
 shutdown.await;
 println!("croakable({}) leaving", s);
}

killswitch was developed to be used together with tokio::select! to create abortable tasks.

Structs

KillSwitch

The kill switch used to signal.

Shutdown

Receiver object for kill switches.

Functions

killswitch

Create a connected KillSwitch and a Shutdown pair.