tokio_proto/simple/pipeline/
mod.rs

1//! Pipelined RPC protocols.
2//!
3//! See the crate-level docs for an overview.
4
5mod client;
6pub use self::client::ClientProto;
7pub use self::client::ClientService;
8
9mod server;
10pub use self::server::ServerProto;
11
12/// A marker used to flag protocols as being pipelined RPC.
13///
14/// This is an implementation detail; to actually implement a protocol,
15/// implement the `ClientProto` or `ServerProto` traits in this module.
16#[derive(Debug)]
17pub struct Pipeline;
18
19// This is a submodule so that `LiftTransport` can be marked `pub`, to satisfy
20// the no-private-in-public checker.
21mod lift {
22    use std::io;
23    use std::marker::PhantomData;
24
25    use streaming::pipeline::{Frame, Transport};
26    use futures::{Future, Stream, Sink, StartSend, Poll, Async, AsyncSink};
27
28    // Lifts an implementation of RPC-style transport to streaming-style transport
29    pub struct LiftTransport<T, E>(pub T, pub PhantomData<E>);
30
31    // Lifts the Bind from the underlying transport
32    pub struct LiftBind<A, F, E> {
33        fut: F,
34        marker: PhantomData<(A, E)>,
35    }
36
37    impl<E, T: Stream<Error = io::Error>> Stream for LiftTransport<T, E> {
38        type Item = Frame<T::Item, (), E>;
39        type Error = io::Error;
40
41        fn poll(&mut self) -> Poll<Option<Self::Item>, io::Error> {
42            let item = try_ready!(self.0.poll());
43            Ok(item.map(|msg| {
44                Frame::Message { message: msg, body: false }
45            }).into())
46        }
47    }
48
49    impl<E, T: Sink<SinkError = io::Error>> Sink for LiftTransport<T, E> {
50        type SinkItem = Frame<T::SinkItem, (), E>;
51        type SinkError = io::Error;
52
53        fn start_send(&mut self, request: Self::SinkItem)
54                      -> StartSend<Self::SinkItem, io::Error> {
55            if let Frame::Message { message, body } = request {
56                if !body {
57                    match try!(self.0.start_send(message)) {
58                        AsyncSink::Ready => return Ok(AsyncSink::Ready),
59                        AsyncSink::NotReady(msg) => {
60                            let msg = Frame::Message { message: msg, body: false };
61                            return Ok(AsyncSink::NotReady(msg))
62                        }
63                    }
64                }
65            }
66            Err(io::Error::new(io::ErrorKind::Other, "no support for streaming"))
67        }
68
69        fn poll_complete(&mut self) -> Poll<(), io::Error> {
70            self.0.poll_complete()
71        }
72
73        fn close(&mut self) -> Poll<(), io::Error> {
74            self.0.close()
75        }
76    }
77
78    impl<T, E: 'static> Transport for LiftTransport<T, E>
79        where T: 'static + Stream<Error = io::Error> + Sink<SinkError = io::Error>
80    {}
81
82    impl<A, F, E> LiftBind<A, F, E> {
83        pub fn lift(f: F) -> LiftBind<A, F, E> {
84            LiftBind {
85                fut: f,
86                marker: PhantomData,
87            }
88        }
89    }
90
91    impl<A, F, E> Future for LiftBind<A, F, E> where F: Future<Error = io::Error> {
92        type Item = LiftTransport<F::Item, E>;
93        type Error = io::Error;
94
95        fn poll(&mut self) -> Poll<Self::Item, io::Error> {
96            Ok(Async::Ready(LiftTransport(try_ready!(self.fut.poll()), PhantomData)))
97        }
98    }
99}