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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use futures::channel::mpsc::{self, Receiver, Sender};
use futures::{Sink, SinkExt, Stream, StreamExt};
use pin_project::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::{FatalErr, Pair};
pub async fn connect<S, R, E>(
left: impl Pair<S, R, E>,
right: impl Pair<R, S, E>,
) -> Result<(), FatalErr> {
let (to_l, from_l) = left.split();
let (to_r, from_r) = right.split();
let l2r = from_l.map(Ok).forward(to_r);
let r2l = from_r.map(Ok).forward(to_l);
tokio::select! {
res = l2r => res,
res = r2l => res,
}
.map_err(Into::into)
}
pub fn passthru<S, R>() -> (MpscPair<S, R>, MpscPair<R, S>) {
let (prod_w, cons_r) = mpsc::channel::<S>(1);
let (cons_w, prod_r) = mpsc::channel::<R>(1);
let prod = ConcretePair::from_parts(prod_w, prod_r);
let cons = ConcretePair::from_parts(cons_w, cons_r);
(prod, cons)
}
pub fn passthru_pair<S, R, E>() -> (impl Pair<S, R, E> + Unpin, impl Pair<R, S, E> + Unpin)
where
S: Send,
R: Send,
E: Send,
{
let (a, b) = passthru::<Result<S, E>, Result<R, E>>();
(a.sink_err_into(), b.sink_err_into())
}
#[pin_project]
pub struct ConcretePair<SS, RS> {
#[pin]
sender: SS,
#[pin]
receiver: RS,
}
pub type MpscPair<S, R> = ConcretePair<Sender<S>, Receiver<R>>;
impl<S> ConcretePair<Sender<S>, Receiver<S>> {
pub fn pipe() -> Self {
let (snd, rcv) = mpsc::channel::<S>(1);
Self::from_parts(snd, rcv)
}
}
impl<SS, RS> ConcretePair<SS, RS> {
pub fn from_parts<S>(sender: SS, receiver: RS) -> Self
where
SS: Sink<S>,
RS: Stream,
{
Self { sender, receiver }
}
pub fn into_parts(self) -> (SS, RS) {
(self.sender, self.receiver)
}
}
impl<SS, RS> Stream for ConcretePair<SS, RS>
where
RS: Stream,
{
type Item = RS::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().receiver.poll_next(cx)
}
}
impl<S, SS, RS> Sink<S> for ConcretePair<SS, RS>
where
SS: Sink<S>,
{
type Error = SS::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().sender.poll_ready(cx)
}
fn start_send(self: Pin<&mut Self>, item: S) -> Result<(), Self::Error> {
self.project().sender.start_send(item)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().sender.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().sender.poll_close(cx)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::JsonRpcErr;
use futures::StreamExt;
use rstest::rstest;
use serde_json::{json, Value};
#[rstest]
#[case::drop_s1(0)]
#[case::drop_r2(1)]
#[tokio::test]
async fn test_connect(#[case] which_to_drop: usize) {
let (mut s1, r1) = passthru_pair::<Value, (), JsonRpcErr>();
let (s2, mut r2) = passthru_pair::<Value, (), JsonRpcErr>();
let handle = tokio::spawn(async move { connect(r1, s2).await });
r2.send(Ok(())).await.unwrap();
assert_eq!(s1.next().await.transpose().unwrap(), Some(()));
s1.send(Ok(json!("asdf"))).await.unwrap();
assert_eq!(
r2.next().await.transpose().unwrap(),
Some(Value::String("asdf".to_owned()))
);
if which_to_drop == 0 {
drop(s1);
} else {
drop(r2);
}
handle.await.unwrap().unwrap();
}
#[tokio::test]
async fn test_passthru() {
let (mut left, right) = passthru::<Value, Value>();
let (r_snd, r_rcv) = right.split();
tokio::spawn(async move { r_rcv.map(Ok).forward(r_snd).await });
left.send(json!("qwer")).await.unwrap();
assert_eq!(left.next().await, Some(Value::String("qwer".to_owned())));
}
#[tokio::test]
async fn test_pipe() {
let p = ConcretePair::pipe();
let (mut p_snd, p_rcv) = p.into_parts();
let mut p_rcv = p_rcv.map(Value::String);
p_snd.send("zxcv".to_owned()).await.unwrap();
assert_eq!(p_rcv.next().await, Some(Value::String("zxcv".to_owned())));
}
}