ggstd/compat/
readers.rs

1use crate::io as ggio;
2
3impl<R: std::io::Read> ggio::ByteReader for std::io::BufReader<R> {
4    fn read_byte(&mut self) -> std::io::Result<u8> {
5        read_byte(self)
6    }
7}
8
9/// read_byte reads and returns the next byte from the input or
10/// any error encountered. If read_byte returns an error, no input
11/// byte was consumed.
12///
13/// This function can be used to implement crate::io::ByteReader trait
14/// for structures that implement std::io::BufRead interface.
15pub fn read_byte<T: std::io::BufRead + ?Sized>(r: &mut T) -> std::io::Result<u8> {
16    let buf = match r.fill_buf() {
17        Ok(buf) => buf,
18        Err(err) => return Err(err),
19    };
20    if buf.is_empty() {
21        return Err(std::io::Error::from(std::io::ErrorKind::UnexpectedEof));
22    }
23    let b = buf[0];
24    r.consume(1);
25    Ok(b)
26}