-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbyte_channel.rs
187 lines (145 loc) Β· 4.49 KB
/
byte_channel.rs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use io::Write;
use std::{
cmp, io,
pin::Pin,
task::{Context, Poll, Waker},
};
use tokio::io::AsyncRead;
//// An in-memory buffer with a sync write and an async read half
pub(super) struct ByteChannel {
buff: Vec<u8>,
read_wakers: Vec<Waker>,
shutdown: bool,
}
impl ByteChannel {
pub(super) fn new() -> Self {
Self {
buff: vec![],
read_wakers: vec![],
shutdown: false,
}
}
}
impl AsyncRead for ByteChannel {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
if self.buff.len() == 0 {
if self.shutdown {
return Poll::Ready(Ok(0));
}
self.read_wakers.push(cx.waker().clone());
return Poll::Pending;
}
let len = cmp::min(buf.len(), self.buff.len());
buf[..len].copy_from_slice(self.buff.drain(..len).collect::<Vec<u8>>().as_slice());
Poll::Ready(Ok(len))
}
}
impl Write for ByteChannel {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if buf.len() == 0 {
return Ok(0);
}
if self.shutdown {
return Err(io::Error::from(io::ErrorKind::BrokenPipe));
}
self.buff.extend_from_slice(buf);
for i in self.read_wakers.drain(..) {
i.wake();
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl ByteChannel {
pub(super) fn shutdown(&mut self) {
self.shutdown = true;
for i in self.read_wakers.drain(..) {
i.wake();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
use tokio::{io::AsyncReadExt, runtime::Runtime, time::timeout};
#[test]
fn test_new() {
let stream = ByteChannel::new();
assert_eq!(stream.buff, Vec::<u8>::new());
assert_eq!(stream.read_wakers.len(), 0);
}
#[test]
fn test_write() {
let mut stream = ByteChannel::new();
stream.write(&[1, 2, 3]).unwrap();
assert_eq!(stream.buff, vec![1, 2, 3]);
}
#[test]
fn test_write_then_read() {
let mut stream = ByteChannel::new();
let (read, read_buff) = Runtime::new().unwrap().block_on(async {
stream.write(&[1, 2, 3]).unwrap();
let mut read_buff = [0u8; 1024];
let read = stream.read(&mut read_buff).await.unwrap();
(read, read_buff)
});
assert_eq!(read, 3);
assert_eq!(&read_buff[..3], &[1, 2, 3]);
assert_eq!(stream.buff, Vec::<u8>::new());
}
// #[test]
// fn test_read_then_write() {
// let mut stream = Arc::new(Mutex::new(ByteChannel::new()));
// let (read, read_buff) = Runtime::new().unwrap().block_on(async {
// let mut read_stream = Arc::clone(&stream);
// let read_task = tokio::spawn(async move {
// let read_stream = read_stream.lock().unwrap();
// let mut read_buff = [0u8; 1024];
// let read = read_stream.read(&mut read_buff).await.unwrap();
// (read, read_buff)
// });
// let stream = stream.lock().unwrap();
// stream.write(&[1, 2, 3]).unwrap();
// read_task.await.unwrap()
// });
// assert_eq!(read, 3);
// assert_eq!(&read_buff[..3], &[1, 2, 3]);
// let stream = stream.lock().unwrap();
// assert_eq!(stream.buff, Vec::<u8>::new());
// }
#[test]
fn test_read_empty() {
let mut stream = ByteChannel::new();
Runtime::new().unwrap().block_on(async {
let mut read_buff = [0u8; 1024];
timeout(Duration::from_millis(100), stream.read(&mut read_buff))
.await
.expect_err("should await until there is data written");
});
}
#[test]
fn test_read_after_close() {
let mut stream = ByteChannel::new();
Runtime::new().unwrap().block_on(async {
stream.shutdown();
assert_eq!(stream.read(&mut [0u8; 1024]).await.unwrap(), 0);
});
}
#[test]
fn test_write_after_close() {
let mut stream = ByteChannel::new();
Runtime::new().unwrap().block_on(async {
stream.shutdown();
stream
.write(&mut [0u8; 1024])
.expect_err("should return error if writing after closing stream");
});
}
}