-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpanic_guard.rs
65 lines (57 loc) · 1.76 KB
/
panic_guard.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::sync::mpsc;
use std::ops::Drop;
use std::thread;
/// A PanicGuard wraps a Sender<Item=Result<T,()>> and sends an Err(()) through it
/// if the PanicGuard is dropped while the current thread is panicking.
/// This lets the consumer on the other end know immediately that a thread
/// has panicked. (Instead of having to wait until later when we join() the threads.)
pub struct PanicGuard<T, S: Sender<Item=Result<T,()>>>
{
sender: S
}
impl<T, S: Sender<Item=Result<T,()>>> PanicGuard<T,S> {
pub fn new(sender: S) -> Self {
PanicGuard{sender: sender}
}
pub fn send(&self, item: T) -> Result<(), S::Error> {
self.sender.send(Ok(item))
}
}
/// A trait for the common functionality in
/// * mpsc::Sender
/// * mpsc::SyncSender
/// * crossbeam_channel::Sender
pub trait Sender {
type Item;
type Error;
fn send(&self, t: Self::Item) -> Result<(), Self::Error>;
}
impl<T> Sender for mpsc::Sender<T> {
type Item = T;
type Error = mpsc::SendError<Self::Item>;
fn send(&self, t: Self::Item) -> Result<(), Self::Error> {
mpsc::Sender::send(&self, t)
}
}
impl<T> Sender for mpsc::SyncSender<T> {
type Item = T;
type Error = mpsc::SendError<Self::Item>;
fn send(&self, t: Self::Item) -> Result<(), Self::Error> {
mpsc::SyncSender::send(&self, t)
}
}
impl <T> Sender for ::crossbeam_channel::Sender<T> {
type Item = T;
type Error = ::crossbeam_channel::SendError<Self::Item>;
fn send(&self, t: Self::Item) -> Result<(), Self::Error> {
::crossbeam_channel::Sender::send(&self, t)
}
}
impl<T, S: Sender<Item=Result<T,()>>> Drop for PanicGuard<T,S>
{
fn drop(&mut self) {
if thread::panicking() {
let _result = self.sender.send(Err(()));
}
}
}