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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use futures::future::BoxFuture;
use once_cell::sync::OnceCell;
use std::sync::Arc;
pub trait CanSend<M>: Send + Sync + 'static {
fn send(&self, message: M);
}
pub struct Sender<M: 'static> {
sender: Arc<dyn CanSend<M>>,
}
impl<M> Clone for Sender<M> {
fn clone(&self) -> Self {
Self { sender: self.sender.clone() }
}
}
pub trait IntoSender<M> {
fn into_sender(self) -> Sender<M>;
fn as_sender(self: &Arc<Self>) -> Sender<M>;
}
impl<M, T: CanSend<M>> IntoSender<M> for T {
fn into_sender(self) -> Sender<M> {
Sender::from_impl(self)
}
fn as_sender(self: &Arc<Self>) -> Sender<M> {
Sender::from_arc(self.clone())
}
}
impl<M> Sender<M> {
pub fn send(&self, message: M) {
self.sender.send(message)
}
fn from_impl(sender: impl CanSend<M> + 'static) -> Self {
Self { sender: Arc::new(sender) }
}
fn from_arc<T: CanSend<M> + 'static>(arc: Arc<T>) -> Self {
Self { sender: arc }
}
pub fn noop() -> Self {
Self::from_impl(Noop)
}
}
pub trait CanSendAsync<M, R>: Send + Sync + 'static {
fn send_async(&self, message: M) -> BoxFuture<'static, R>;
}
pub struct AsyncSender<M: 'static, R: 'static> {
sender: Arc<dyn CanSendAsync<M, R>>,
}
impl<M, R> Clone for AsyncSender<M, R> {
fn clone(&self) -> Self {
Self { sender: self.sender.clone() }
}
}
pub trait IntoAsyncSender<M, R> {
fn into_async_sender(self) -> AsyncSender<M, R>;
fn as_async_sender(self: &Arc<Self>) -> AsyncSender<M, R>;
}
impl<M, R, T: CanSendAsync<M, R>> IntoAsyncSender<M, R> for T {
fn into_async_sender(self) -> AsyncSender<M, R> {
AsyncSender::from_impl(self)
}
fn as_async_sender(self: &Arc<Self>) -> AsyncSender<M, R> {
AsyncSender::from_arc(self.clone())
}
}
impl<M, R> AsyncSender<M, R> {
pub fn send_async(&self, message: M) -> BoxFuture<'static, R> {
self.sender.send_async(message)
}
fn from_impl(sender: impl CanSendAsync<M, R> + 'static) -> Self {
Self { sender: Arc::new(sender) }
}
fn from_arc<T: CanSendAsync<M, R> + 'static>(arc: Arc<T>) -> Self {
Self { sender: arc }
}
}
pub struct LateBoundSender<S> {
sender: OnceCell<S>,
}
impl<S> Default for LateBoundSender<S> {
fn default() -> Self {
Self { sender: OnceCell::default() }
}
}
impl<S> LateBoundSender<S> {
pub fn bind(&self, sender: S) {
self.sender.set(sender).map_err(|_| ()).expect("cannot set sender twice");
}
}
impl<M, S: CanSend<M>> CanSend<M> for LateBoundSender<S> {
fn send(&self, message: M) {
self.sender.wait().send(message);
}
}
impl<M, R, S: CanSendAsync<M, R>> CanSendAsync<M, R> for LateBoundSender<S> {
fn send_async(&self, message: M) -> BoxFuture<'static, R> {
self.sender.wait().send_async(message)
}
}
struct Noop;
impl<M> CanSend<M> for Noop {
fn send(&self, _message: M) {}
}
impl<A: AsRef<Sender<M>> + Send + Sync + 'static, M: 'static> CanSend<M> for A {
fn send(&self, message: M) {
self.as_ref().send(message)
}
}
impl<A: AsRef<AsyncSender<M, R>> + Send + Sync + 'static, M: 'static, R: 'static> CanSendAsync<M, R>
for A
{
fn send_async(&self, message: M) -> BoxFuture<'static, R> {
self.as_ref().send_async(message)
}
}