1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::io::{self, Read};

use crate::ReadBytes;

/// Extension trait to `Read` to read bytes using endianness.
pub trait ReadExt: Read + Sized {
    /// Read bytes as big endian.
    fn read_be<B: ReadBytes>(&mut self) -> io::Result<B> {
        <B>::read_be_bytes(self)
    }

    /// Read bytes as little endian.
    fn read_le<B: ReadBytes>(&mut self) -> io::Result<B> {
        <B>::read_le_bytes(self)
    }

    /// Read bytes using native endianness.
    fn read_ne<B: ReadBytes>(&mut self) -> io::Result<B> {
        <B>::read_ne_bytes(self)
    }
}

impl<T: Read> ReadExt for T {}