Expand description
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.
use pandet::*;
let detector = PanicDetector::new();
// Whichever async task spawner
task::spawn(
async move {
panic!();
}
.alert(&detector) // 👈 Binds the detector so it is notified of any panic from the future
);
assert!(detector.await.is_some()); // See notes below
!Send
tasks implement the LocalAlert
trait:
use pandet::*;
let detector = PanicDetector::new();
task::spawn_local(
async move {
// Does some work without panicking...
}
.local_alert(&detector)
);
assert!(detector.await.is_none());
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:
use futures::StreamExt;
use pandet::*;
// Any Unpin + Send + 'static type works
struct FailureMsg {
task_id: usize,
}
let mut monitor = PanicMonitor::<FailureMsg>::new(); // Or simply PanicMonitor::new()
for task_id in 0..=10 {
task::spawn(
async move {
if task_id % 3 == 0 {
panic!();
}
}
// Notifies the monitor of the panicked task's ID
.alert_msg(&monitor, FailureMsg { task_id })
);
}
while let Some(Panicked(msg)) = monitor.next().await {
assert_eq!(msg.task_id % 3, 0);
}
Structs
- Notifies
PanicDetector
/PanicMonitor
of panics. - A future that finishes with an
Some(Panicked<Msg>)
when a task has panicked orNone
if no task panicked. - A
Stream
of detected panics. - Created by the panic detector when a panic occurs.
Traits
- Used to bind a
PanicDetector
onto any task. Implemented for allFuture
types.