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
166
167
168
169
170
171
172
173
use bincode;
use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut};
use std::{fmt, io};
use tokio;
use tokio::prelude::*;
use {AsyncBincodeReader, AsyncBincodeWriter};
use {AsyncDestination, BincodeWriterFor, SyncDestination};
#[derive(Debug)]
pub struct AsyncBincodeStream<S, T, D> {
stream: AsyncBincodeReader<InternalAsyncWriter<S, T, D>, T>,
}
struct InternalAsyncWriter<W, T, D>(AsyncBincodeWriter<W, T, D>);
impl<S: fmt::Debug, T, D> fmt::Debug for InternalAsyncWriter<S, T, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.get_ref().fmt(f)
}
}
impl<S, T> Default for AsyncBincodeStream<S, T, SyncDestination>
where
S: Default,
{
fn default() -> Self {
Self::from(S::default())
}
}
impl<S, T, D> AsyncBincodeStream<S, T, D> {
pub fn get_ref(&self) -> &S {
&self.stream.get_ref().0.get_ref()
}
pub fn get_mut(&mut self) -> &mut S {
self.stream.get_mut().0.get_mut()
}
pub fn into_inner(self) -> S {
self.stream.into_inner().0.into_inner()
}
}
impl<S, T> From<S> for AsyncBincodeStream<S, T, SyncDestination> {
fn from(stream: S) -> Self {
AsyncBincodeStream {
stream: AsyncBincodeReader::from(InternalAsyncWriter(AsyncBincodeWriter::from(stream))),
}
}
}
impl<S, T, D> AsyncBincodeStream<S, T, D> {
pub fn for_async(self) -> AsyncBincodeStream<S, T, AsyncDestination> {
let stream = self.into_inner();
AsyncBincodeStream {
stream: AsyncBincodeReader::from(InternalAsyncWriter(
AsyncBincodeWriter::from(stream).for_async(),
)),
}
}
pub fn for_sync(self) -> AsyncBincodeStream<S, T, SyncDestination> {
AsyncBincodeStream::from(self.into_inner())
}
pub fn split(mut self) -> (AsyncBincodeWriter<S, T, D>, AsyncBincodeReader<S, T>)
where
S: Clone,
{
let rbuff = self.stream.buffer.split_off(0);
let size = self.stream.size;
let writer = self.stream.into_inner().0;
let stream = writer.get_ref().clone();
let mut reader = AsyncBincodeReader::from(stream);
reader.buffer = rbuff;
reader.size = size;
(writer, reader)
}
}
impl<S, T, D> tokio::io::AsyncRead for InternalAsyncWriter<S, T, D>
where
S: tokio::io::AsyncRead,
{
}
impl<S, T, D> io::Read for InternalAsyncWriter<S, T, D>
where
S: Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.get_mut().read(buf)
}
}
impl<S, T, D> Deref for InternalAsyncWriter<S, T, D> {
type Target = AsyncBincodeWriter<S, T, D>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<S, T, D> DerefMut for InternalAsyncWriter<S, T, D> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<S, T, D> Stream for AsyncBincodeStream<S, T, D>
where
for<'a> T: Deserialize<'a>,
S: tokio::io::AsyncRead,
{
type Item = T;
type Error = bincode::Error;
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
self.stream.poll()
}
}
impl<S, T, D> Sink for AsyncBincodeStream<S, T, D>
where
T: Serialize,
S: tokio::io::AsyncWrite,
AsyncBincodeWriter<S, T, D>: BincodeWriterFor<T>,
{
type SinkItem = T;
type SinkError = bincode::Error;
fn start_send(
&mut self,
item: Self::SinkItem,
) -> Result<AsyncSink<Self::SinkItem>, Self::SinkError> {
self.stream.get_mut().start_send(item)
}
fn poll_complete(&mut self) -> Result<Async<()>, Self::SinkError> {
self.stream.get_mut().poll_complete()
}
}