-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.rs
48 lines (41 loc) · 1.03 KB
/
read.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
use std::io;
pub(crate) trait Read {
fn read(&mut self, len: usize) -> io::Result<&[u8]>;
}
pub(crate) struct IoReader<R: io::Read> {
rdr: R,
buf: Vec<u8>,
}
impl<R: io::Read> IoReader<R> {
pub fn new(rdr: R) -> Self {
IoReader { rdr, buf: Vec::new() }
}
}
impl<R: io::Read> Read for IoReader<R> {
fn read(&mut self, len: usize) -> io::Result<&[u8]> {
unsafe {
self.buf.reserve(len);
let slice = self.buf.get_unchecked_mut(..len);
self.rdr.read_exact(slice)?;
Ok(slice)
}
}
}
pub(crate) struct SliceReader<'a> {
slice: &'a [u8],
}
impl<'a> SliceReader<'a> {
pub fn new(slice: &'a [u8]) -> Self {
SliceReader { slice }
}
}
impl<'a> Read for SliceReader<'a> {
fn read(&mut self, len: usize) -> io::Result<&[u8]> {
if len > self.slice.len() {
return Err(io::ErrorKind::UnexpectedEof.into())
}
let (head, tail) = self.slice.split_at(len);
self.slice = tail;
Ok(head)
}
}