read_specific

Function read_specific 

Source
pub fn read_specific<R, E>(reader: &mut R) -> Result<E>
where R: Read + ?Sized, E: EndianRead,
Available on crate feature io-std only.
Expand description

Read an endian-aware value of type E from a reader.

This helper works with both sized readers (e.g. std::io::Cursor<Vec<u8>>) and unsized trait objects like &mut dyn std::io::Read.

In particular, this is designed to support the common “extension trait” pattern:

use std::io::{self, Read};

pub trait ReadBytesExt: Read {
    fn read_u32_be(&mut self) -> io::Result<u32>;
}

impl<R: Read + ?Sized> ReadBytesExt for R {
    fn read_u32_be(&mut self) -> io::Result<u32> {
        let be: simple_endian::BigEndian<u32> = simple_endian::read_specific(self)?;
        Ok(be.to_native())
    }
}

fn read_from_dyn(r: &mut dyn Read) -> io::Result<u32> {
    r.read_u32_be()
}