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
extern crate futures; extern crate bytes; extern crate serde; extern crate rmp_serde; extern crate tokio_serde; use futures::{Stream, Poll, Sink, StartSend}; use bytes::{Bytes, BytesMut, Buf, IntoBuf}; use serde::{Serialize, Deserialize}; use tokio_serde::{Serializer, Deserializer, FramedRead, FramedWrite}; use std::marker::PhantomData; struct MsgPack<T> { ghost: PhantomData<T>, } fn into_io_error<U, E>(res: Result<U,E>) -> Result<U, std::io::Error> where E: Into<Box<std::error::Error + Send + Sync>>, { res.map_err(|err| { std::io::Error::new(std::io::ErrorKind::Other, err) }) } impl<T> Deserializer<T> for MsgPack<T> where for <'a> T: Deserialize<'a>, { type Error = std::io::Error; fn deserialize(&mut self, src: &Bytes) -> Result<T, Self::Error> { into_io_error(rmp_serde::decode::from_read(src.into_buf().reader())) } } impl<T: Serialize> Serializer<T> for MsgPack<T> { type Error = std::io::Error; fn serialize(&mut self, item: &T) -> Result<BytesMut, Self::Error> { into_io_error(rmp_serde::encode::to_vec(item).map(Into::into)) } } pub struct ReadMsgPack<T,U> { inner: FramedRead<T, U, MsgPack<U>>, } impl<T, U> ReadMsgPack<T, U> where T: Stream, T::Error: From<std::io::Error>, for<'a> U: Deserialize<'a>, Bytes: From<T::Item>, { pub fn new(inner: T) -> ReadMsgPack<T, U> { let mp = MsgPack { ghost: PhantomData }; ReadMsgPack { inner: FramedRead::new(inner, mp) } } } impl<T, U> ReadMsgPack<T, U> { pub fn get_ref(&self) -> &T { self.inner.get_ref() } pub fn get_mut(&mut self) -> &mut T { self.inner.get_mut() } pub fn into_inner(self) -> T { self.inner.into_inner() } } impl<T, U> Stream for ReadMsgPack<T, U> where T: Stream, T::Error: From<std::io::Error>, for<'a> U: Deserialize<'a>, Bytes: From<T::Item>, { type Item = U; type Error = T::Error; fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { self.inner.poll() } } impl<T, U> Sink for ReadMsgPack<T, U> where T: Sink, { type SinkItem = T::SinkItem; type SinkError = T::SinkError; fn start_send(&mut self, item: T::SinkItem) -> StartSend<T::SinkItem, T::SinkError> { self.get_mut().start_send(item) } fn poll_complete(&mut self) -> Poll<(), T::SinkError> { self.get_mut().poll_complete() } fn close(&mut self) -> Poll<(), T::SinkError> { self.get_mut().close() } } pub struct WriteMsgPack<T: Sink, U> { inner: FramedWrite<T, U, MsgPack<U>>, } impl<T, U> WriteMsgPack<T, U> where T: Sink<SinkItem = BytesMut>, T::SinkError: From<std::io::Error>, U: Serialize, { pub fn new(inner: T) -> WriteMsgPack<T, U> { let mp = MsgPack { ghost: PhantomData }; let fw = FramedWrite::new(inner, mp); WriteMsgPack { inner: fw } } } impl<T: Sink, U> WriteMsgPack<T, U> { pub fn get_ref(&self) -> &T { self.inner.get_ref() } pub fn get_mut(&mut self) -> &mut T { self.inner.get_mut() } pub fn into_inner(self) -> T { self.inner.into_inner() } } impl<T, U> Stream for WriteMsgPack<T, U> where T: Stream + Sink, { type Item = T::Item; type Error = T::Error; fn poll(&mut self) -> Poll<Option<T::Item>, T::Error> { self.get_mut().poll() } } impl<T, U> Sink for WriteMsgPack<T, U> where T: Sink<SinkItem = BytesMut>, T::SinkError: From<std::io::Error>, U: Serialize, { type SinkItem = U; type SinkError = T::SinkError; fn start_send(&mut self, item: U) -> StartSend<U, T::SinkError> { self.inner.start_send(item) } fn poll_complete(&mut self) -> Poll<(), T::SinkError> { self.inner.poll_complete() } fn close(&mut self) -> Poll<(), T::SinkError> { self.inner.poll_complete() } }