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
#[cfg(target_family = "wasm")]
mod wasm;
use std::task::{Context, Poll};
use futures::{Future, FutureExt};
#[cfg(target_family = "wasm")]
pub use wasm::*;
#[cfg(not(target_family = "wasm"))]
mod native;
#[cfg(not(target_family = "wasm"))]
pub use native::*;
use crate::Error;
#[must_use]
pub fn unbounded_channel<Item>() -> (UnboundedSender<Item>, UnboundedReceiver<Item>)
where
Item: ConditionallySafe,
{
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
(UnboundedSender(tx), UnboundedReceiver(rx))
}
#[allow(missing_debug_implementations)]
pub struct UnboundedSender<Item>(tokio::sync::mpsc::UnboundedSender<Item>)
where
Item: ConditionallySafe;
impl<Item> Clone for UnboundedSender<Item>
where
Item: ConditionallySafe,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<Item> UnboundedSender<Item>
where
Item: ConditionallySafe,
{
pub fn send(&self, message: Item) -> Result<(), Error> {
self.0.send(message).map_err(|_| Error::SendFailed(0))
}
#[must_use]
pub fn is_closed(&self) -> bool {
self.0.is_closed()
}
}
#[allow(missing_debug_implementations)]
pub struct UnboundedReceiver<Item>(tokio::sync::mpsc::UnboundedReceiver<Item>)
where
Item: ConditionallySafe;
impl<Item> UnboundedReceiver<Item>
where
Item: ConditionallySafe,
{
pub async fn recv(&mut self) -> Option<Item> {
self.0.recv().await
}
pub fn poll_recv(&mut self, cx: &mut Context) -> Poll<Option<Item>> {
self.0.poll_recv(cx)
}
}
#[must_use]
pub fn oneshot<Item>() -> (OneShotSender<Item>, OneShotReceiver<Item>)
where
Item: ConditionallySafe,
{
let (tx, rx) = tokio::sync::oneshot::channel();
(OneShotSender(tx), OneShotReceiver(rx))
}
#[allow(missing_debug_implementations)]
pub struct OneShotSender<Item>(tokio::sync::oneshot::Sender<Item>)
where
Item: ConditionallySafe;
impl<Item> OneShotSender<Item>
where
Item: ConditionallySafe,
{
pub fn send(self, message: Item) -> Result<(), Error> {
self.0.send(message).map_err(|_| Error::SendFailed(0))
}
#[must_use]
pub fn is_closed(&self) -> bool {
self.0.is_closed()
}
}
#[allow(missing_debug_implementations)]
pub struct OneShotReceiver<Item>(tokio::sync::oneshot::Receiver<Item>)
where
Item: ConditionallySafe;
impl<Item> OneShotReceiver<Item>
where
Item: ConditionallySafe,
{
pub async fn recv(self) -> Result<Item, Error> {
self.0.await.map_err(|_e| Error::RecvFailed(80))
}
}
impl<Item> Future for OneShotReceiver<Item>
where
Item: ConditionallySafe,
{
type Output = Result<Item, Error>;
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let poll = self.get_mut().0.poll_unpin(cx);
poll.map_err(|_e| Error::RecvFailed(95))
}
}