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
use futures_core::{
stream::{Stream, TryStream},
task::{Context, Poll},
};
use futures_sink::Sink;
use pin_project::pin_project;
use std::pin::Pin;
pub mod client;
pub use self::client::{Client, TagStore};
pub mod server;
pub use self::server::Server;
#[pin_project]
#[derive(Debug)]
pub struct MultiplexTransport<T, S> {
#[pin]
transport: T,
#[pin]
tagger: S,
}
impl<T, S> MultiplexTransport<T, S> {
pub fn new(transport: T, tagger: S) -> Self {
MultiplexTransport { transport, tagger }
}
}
impl<T, S, Request> Sink<Request> for MultiplexTransport<T, S>
where
T: Sink<Request>,
{
type Error = <T as Sink<Request>>::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().transport.poll_ready(cx)
}
fn start_send(self: Pin<&mut Self>, item: Request) -> Result<(), Self::Error> {
self.project().transport.start_send(item)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().transport.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().transport.poll_close(cx)
}
}
impl<T, S> Stream for MultiplexTransport<T, S>
where
T: TryStream,
{
type Item = Result<<T as TryStream>::Ok, <T as TryStream>::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().transport.try_poll_next(cx)
}
}
impl<T, S, Request> TagStore<Request, <T as TryStream>::Ok> for MultiplexTransport<T, S>
where
T: Sink<Request> + TryStream,
S: TagStore<Request, <T as TryStream>::Ok>,
{
type Tag = <S as TagStore<Request, <T as TryStream>::Ok>>::Tag;
fn assign_tag(self: Pin<&mut Self>, req: &mut Request) -> Self::Tag {
self.project().tagger.assign_tag(req)
}
fn finish_tag(self: Pin<&mut Self>, rsp: &<T as TryStream>::Ok) -> Self::Tag {
self.project().tagger.finish_tag(rsp)
}
}