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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::*;
pub type Sender<T> = std::sync::mpsc::Sender<T>;
pub type SyncSender<T> = std::sync::mpsc::SyncSender<T>;
pub type Receiver<T> = std::sync::mpsc::Receiver<T>;
pub type TryRecvError = std::sync::mpsc::TryRecvError;
pub type RecvError = std::sync::mpsc::RecvError;
pub type SendError<T> = std::sync::mpsc::SendError<T>;
pub type TrySendError<T> = std::sync::mpsc::TrySendError<T>;
#[derive(Clone, Debug, PartialEq)]
pub enum DownMsg<Down: Send> {
Stop,
Pause,
Continue,
Other(Down)
}
#[derive(Clone, Debug)]
pub struct UpMsg<Up: Send> {
time: Instant,
msg: Up
}
#[derive(Clone, Debug)]
pub struct WorkerSender<Up: Send> {
sender: SyncSender<UpMsg<Up>>,
}
impl<Up: Send> UpMsg<Up> {
pub fn new(msg: Up) -> Self {
Self {
time: Instant::now(),
msg
}
}
pub fn time(&self) -> Instant {
self.time
}
pub fn get(self) -> Up {
self.msg
}
}
impl<Up: Send> WorkerSender<Up> {
pub(crate) fn new(sender: SyncSender<UpMsg<Up>>) -> Self {
Self {
sender
}
}
pub fn send(&self, msg: Up) -> Result<(), SendError<Up>> {
self.sender.send(UpMsg::new(msg)).map_err(|e| std::sync::mpsc::SendError(e.0.get()))
}
pub fn try_send(&self, msg: Up) -> Result<(), TrySendError<Up>> {
use std::sync::mpsc::TrySendError;
self.sender.try_send(UpMsg::new(msg)).map_err(|e| {
match e {
TrySendError::Full(x) => TrySendError::Full(x.get()),
TrySendError::Disconnected(x) => TrySendError::Disconnected(x.get())
}
})
}
}
impl<Up: Send> !Send for WorkerSender<Up> {}
pub type WorkerReceiver<Down> = Receiver<DownMsg<Down>>;