This commit is contained in:
Carl Lerche
2017-03-10 13:02:04 -08:00
parent 1fe3a57338
commit e2871d92fa
12 changed files with 559 additions and 38 deletions

43
src/proto/framed_read.rs Normal file
View File

@@ -0,0 +1,43 @@
use ConnectionError;
use frame::Frame;
use futures::*;
use bytes::BytesMut;
use std::io;
pub struct FramedRead<T> {
inner: T,
}
impl<T> Stream for FramedRead<T>
where T: Stream<Item = BytesMut, Error = io::Error>,
{
type Item = Frame;
type Error = ConnectionError;
fn poll(&mut self) -> Poll<Option<Frame>, ConnectionError> {
match try_ready!(self.inner.poll()) {
Some(bytes) => {
Frame::load(bytes.freeze())
.map(|frame| Async::Ready(Some(frame)))
.map_err(ConnectionError::from)
}
None => Ok(Async::Ready(None)),
}
}
}
impl<T: Sink> Sink for FramedRead<T> {
type SinkItem = T::SinkItem;
type SinkError = T::SinkError;
fn start_send(&mut self, item: T::SinkItem) -> StartSend<T::SinkItem, T::SinkError> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), T::SinkError> {
self.inner.poll_complete()
}
}