#io-read #peek #eof #peeky #we-re

yanked peeky-read

Peek into an io::Read and see if we're at the EOF

Uses old Rust 2015

0.1.0 Jun 27, 2017

#8 in #eof

MIT license

5KB
94 lines

peeky-read

A single-struct library providing PeekyRead.

PeekyRead takes ownership of an io::Read, and provides a check_eof() -> io::Result<bool>.

This is accomplished by actually reading a single byte from the underlying reader. The byte is stored, and returned automatically by the next read(), so nothing is lost, and the transition should be transparent.


lib.rs:

Wrap an io::Read and provide check_eof(). Returns errors iff the underlying reader does.

Examples

use std::io::Read;
use peeky_read::PeekyRead;
let mut underlying = std::io::Cursor::new([0u8; 1]);
let mut reader = PeekyRead::new(&mut underlying);

// We're not at the EOF as there's bytes to read.
assert_eq!(false, reader.check_eof().unwrap());
let mut buf = [0u8; 32];
assert_eq!(1, reader.read(&mut buf).unwrap());

// We've read the only byte in the reader, so we're now at the EOF.
assert_eq!(true, reader.check_eof().unwrap());

No runtime deps