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
mod client;
pub use self::client::ClientProto;
pub use self::client::ClientService;
mod server;
pub use self::server::ServerProto;
pub type RequestId = u64;
pub struct Multiplex;
mod lift {
use std::io;
use std::marker::PhantomData;
use super::RequestId;
use streaming::multiplex::{Frame, Transport};
use futures::{Future, Stream, Sink, StartSend, Poll, Async, AsyncSink};
pub struct LiftTransport<T, E>(pub T, pub PhantomData<E>);
pub struct LiftBind<A, F, E> {
fut: F,
marker: PhantomData<(A, E)>,
}
impl<T, InnerItem, E> Stream for LiftTransport<T, E> where
E: 'static,
T: Stream<Item = (RequestId, InnerItem), Error = io::Error>,
{
type Item = Frame<InnerItem, (), E>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, io::Error> {
let (id, msg) = match try_ready!(self.0.poll()) {
Some(msg) => msg,
None => return Ok(None.into()),
};
Ok(Some(Frame::Message {
message: msg,
body: false,
solo: false,
id: id,
}).into())
}
}
impl<T, InnerSink, E> Sink for LiftTransport<T, E> where
E: 'static,
T: Sink<SinkItem = (RequestId, InnerSink), SinkError = io::Error>
{
type SinkItem = Frame<InnerSink, (), E>;
type SinkError = io::Error;
fn start_send(&mut self, request: Self::SinkItem)
-> StartSend<Self::SinkItem, io::Error> {
if let Frame::Message { message, id, body, solo } = request {
if !body && !solo {
match try!(self.0.start_send((id, message))) {
AsyncSink::Ready => return Ok(AsyncSink::Ready),
AsyncSink::NotReady((id, msg)) => {
let msg = Frame::Message {
message: msg,
id: id,
body: false,
solo: false,
};
return Ok(AsyncSink::NotReady(msg))
}
}
}
}
Err(io::Error::new(io::ErrorKind::Other, "no support for streaming"))
}
fn poll_complete(&mut self) -> Poll<(), io::Error> {
self.0.poll_complete()
}
}
impl<T, InnerItem, InnerSink, E> Transport<()> for LiftTransport<T, E> where
E: 'static,
T: 'static,
T: Stream<Item = (RequestId, InnerItem), Error = io::Error>,
T: Sink<SinkItem = (RequestId, InnerSink), SinkError = io::Error>
{}
impl<A, F, E> LiftBind<A, F, E> {
pub fn lift(f: F) -> LiftBind<A, F, E> {
LiftBind {
fut: f,
marker: PhantomData,
}
}
}
impl<A, F, E> Future for LiftBind<A, F, E> where F: Future<Error = io::Error> {
type Item = LiftTransport<F::Item, E>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, io::Error> {
Ok(Async::Ready(LiftTransport(try_ready!(self.fut.poll()), PhantomData)))
}
}
}