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. Calling
dropdetector()allows it to finish with
a
Ok(())if no task panics as long as all the other
PanicDetectors 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::
while let Some(res) = monitor.dropdetector().next().await { if let Err(e) = res { let info = e.0; asserteq!(info.task_id, 10); break; } } ```