Crates.io | pandet |
lib.rs | pandet |
version | 0.4.0 |
source | src |
created_at | 2022-04-30 09:55:45.637243 |
updated_at | 2023-03-22 14:33:21.146982 |
description | A lightweight library to help act on panics of detached async tasks |
homepage | |
repository | https://github.com/kkaatii/pandet/ |
max_upload_size | |
id | 578068 |
size | 18,816 |
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());
!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 message to the detector/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 })
);
}
let cnt = 0;
while let Some(Panicked(msg)) = monitor.next().await {
cnt += 1;
assert_eq!(msg.task_id % 3, 0);
}
assert_eq!(cnt, 4);