blob: ae02155003f608d5f3ef4a26e0f4fbb242c6ec94 (
plain)
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
|
use async_std::prelude::*;
use async_std::io::BufReader;
use async_std::io::Read;
pub struct PeekableBufReader<T>
where T: Read + Unpin {
reader: BufReader<T>,
buffer: [u8; 1],
peeked_last: bool,
}
impl<T> PeekableBufReader<T>
where T: Read + Unpin {
pub fn new(reader: BufReader<T>) -> Self {
Self {
reader,
buffer: [0],
peeked_last: false,
}
}
pub async fn next(&mut self) -> Option<u8> {
if self.peeked_last {
self.peeked_last = false;
Some(self.buffer[0])
} else {
match self.reader.read(&mut self.buffer).await.ok()? {
1 => Some(self.buffer[0]),
_ => None,
}
}
}
pub async fn peek(&mut self) -> Option<&u8> {
if self.peeked_last {
Some(&self.buffer[0])
} else {
match self.reader.read(&mut self.buffer).await.ok()? {
1 => {
self.peeked_last = true;
Some(&self.buffer[0])
},
_ => None,
}
}
}
}
|