pandet

version documentation

A lightweight library that helps you detect failure of spawned async tasks without having to .await their handles. Useful when you are spawning lots of detached tasks but want to fast-fail if a panic occurs.

```rust use pandet::{PanicAlert, OnPanic};

let mut alert = PanicAlert::new();

// Whichever async task spawner task::spawn( async move { panic!(); } .onpanic(&alert.newdetector()) // 👈 Binds the alert's detector );

assert!(alert.dropdetector().await.iserr()); // See notes below `` IMPORTANT NOTE: Directly.awaiting an alert is possible, but in this case the alert as a future will only finish when a task panics. Callingdropdetector()allows it to finish with aOk(())if no task panics as long as all the otherPanicDetectors paired with the alert has gone out of scope. See [PanicAlert::dropdetector] and [PanicMonitor::drop_detector`] for more details.

For !Send tasks, there is the UnsendOnPanic trait: ```rust use pandet::{PanicAlert, UnsendOnPanic};

let mut alert = PanicAlert::new();

task::spawnlocal( async move { panic!(); } .unsendonpanic(&alert.newdetector()) );

assert!(alert.dropdetector().await.iserr()); ```

Refined control over how to handle panics can also be implemented with PanicMonitor which works like a stream of alerts. You may also pass some information to the alert/monitor when a panic occurs: ```rust use futures::StreamExt; use pandet::{PanicMonitor, OnPanic};

// Any Unpin + Send + 'static type works struct PanicInfo { task_id: usize, }

let mut monitor = PanicMonitor::::new(); // Or simply PanicMonitor::new() { let detector = monitor.newdetector(); for taskid in 0..=10 { task::spawn( async move { if taskid == 10 { panic!(); } } // Informs the monitor of which task panicked .onpanicinfo(&detector, PanicInfo { taskid }) ); } } // detector goes out of scope, allowing the monitor to finish after calling drop_detector()

while let Some(res) = monitor.dropdetector().next().await { if let Err(e) = res { let info = e.0; asserteq!(info.task_id, 10); break; } } ```