pandet

Crates.iopandet
lib.rspandet
version0.4.0
sourcesrc
created_at2022-04-30 09:55:45.637243
updated_at2023-03-22 14:33:21.146982
descriptionA lightweight library to help act on panics of detached async tasks
homepage
repositoryhttps://github.com/kkaatii/pandet/
max_upload_size
id578068
size18,816
(kkaatii)

documentation

README

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.

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);
Commit count: 13

cargo fmt