-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbincode_codec.rs
More file actions
149 lines (125 loc) · 3.28 KB
/
bincode_codec.rs
File metadata and controls
149 lines (125 loc) · 3.28 KB
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
use std::marker::PhantomData;
use std::io;
use bincode;
use bytes::{Bytes, BytesMut};
use futures::{Poll, Sink, StartSend, Stream};
use serde::{Deserialize, Serialize};
use tokio_serde::{Deserializer, FramedRead, FramedWrite, Serializer};
/// Bincode
struct Bincode<T> {
ghost: PhantomData<T>,
}
pub struct BincodeReader<T, U> {
inner: FramedRead<T, U, Bincode<U>>,
}
pub struct BincodeWriter<T: Sink, U> {
inner: FramedWrite<T, U, Bincode<U>>,
}
impl<T, U> BincodeReader<T, U>
where
T: Stream<Error = io::Error>,
for<'a> U: Deserialize<'a>,
BytesMut: From<T::Item>,
{
pub fn new(inner: T) -> BincodeReader<T, U> {
let bincode = Bincode { ghost: PhantomData };
BincodeReader {
inner: FramedRead::new(inner, bincode),
}
}
}
impl<T, U> BincodeReader<T, U> {
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
impl<T, U> Stream for BincodeReader<T, U>
where
T: Stream<Error = io::Error>,
for<'a> U: Deserialize<'a>,
BytesMut: From<T::Item>,
{
type Item = U;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<U>, io::Error> {
self.inner.poll()
}
}
impl<T, U> Sink for BincodeReader<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()
}
}
impl<T, U> BincodeWriter<T, U>
where
T: Sink<SinkItem = Bytes, SinkError = io::Error>,
U: Serialize,
{
pub fn new(inner: T) -> BincodeWriter<T, U> {
let bincode = Bincode { ghost: PhantomData };
BincodeWriter {
inner: FramedWrite::new(inner, bincode),
}
}
}
impl<T: Sink, U> BincodeWriter<T, U> {
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
impl<T, U> Sink for BincodeWriter<T, U>
where
T: Sink<SinkItem = Bytes, SinkError = io::Error>,
U: Serialize,
{
type SinkItem = U;
type SinkError = io::Error;
fn start_send(&mut self, item: U) -> StartSend<U, io::Error> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), io::Error> {
self.inner.poll_complete()
}
fn close(&mut self) -> Poll<(), io::Error> {
self.inner.close()
}
}
impl<T, U> Stream for BincodeWriter<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> Deserializer<T> for Bincode<T>
where
for<'a> T: Deserialize<'a>,
{
type Error = io::Error;
fn deserialize(&mut self, src: &BytesMut) -> Result<T, io::Error> {
bincode::deserialize(src)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}
}
impl<T: Serialize> Serializer<T> for Bincode<T> {
type Error = io::Error;
fn serialize(&mut self, item: &T) -> Result<Bytes, io::Error> {
bincode::serialize(item)
.map(Into::into)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}
}