use std::io;
use std::io::Read;
pub struct ShortRead<R: Read, I: Iterator<Item = usize>> {
inner: R,
decider: I,
}
impl<R: Read, I: Iterator<Item = usize>> Read for ShortRead<R, I> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let wanted = match self.decider.next() {
Some(0) => return Err(io::Error::from(io::ErrorKind::Interrupted)),
Some(wanted) => wanted,
None => return Ok(0),
};
let wanted = wanted.min(buf.len());
let buf = &mut buf[..wanted];
self.inner.read(buf)
}
}
impl<R: Read, I: Iterator<Item = usize>> ShortRead<R, I> {
pub fn new(inner: R, decider: I) -> Self {
ShortRead { inner, decider }
}
pub fn into_inner(self) -> R {
self.inner
}
}
#[cfg(test)]
mod tests {
use crate::short::ShortRead;
use std::io;
use std::io::Read;
#[test]
fn shorten() {
let mut naughty = ShortRead::new(
io::Cursor::new(b"1234567890"),
vec![2, 3, 4, 5, 6].into_iter(),
);
let mut buf = [0u8; 10];
assert_eq!(2, naughty.read(&mut buf).unwrap());
assert_eq!(b"12", &buf[..2]);
assert_eq!(3, naughty.read(&mut buf).unwrap());
assert_eq!(b"345", &buf[..3]);
assert_eq!(4, naughty.read(&mut buf).unwrap());
assert_eq!(b"6789", &buf[..4]);
assert_eq!(1, naughty.read(&mut buf).unwrap());
assert_eq!(b"0", &buf[..1]);
assert_eq!(0, naughty.read(&mut buf).unwrap());
assert_eq!(0, naughty.read(&mut buf).unwrap());
}
#[test]
fn interrupt() {
let mut interrupting = ShortRead::new(io::Cursor::new(b"12"), vec![0, 1, 0, 1].into_iter());
let mut buf = [0; 1];
assert_eq!(
io::ErrorKind::Interrupted,
interrupting.read(&mut buf).unwrap_err().kind()
);
assert_eq!(1, interrupting.read(&mut buf).unwrap());
assert_eq!(
io::ErrorKind::Interrupted,
interrupting.read(&mut buf).unwrap_err().kind()
);
assert_eq!(1, interrupting.read(&mut buf).unwrap());
}
#[test]
fn std_bufreader_leaves_us_alone() {
use std::io::BufRead;
let mut naughty = io::BufReader::new(ShortRead::new(
io::Cursor::new(b"1234567890"),
vec![2, 3, 4, 5, 6].into_iter(),
));
assert_eq!(b"12", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"2", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"345", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"45", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"5", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"6789", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"789", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"89", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"9", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"0", naughty.fill_buf().unwrap());
naughty.consume(1);
assert_eq!(b"", naughty.fill_buf().unwrap());
}
}