1use std::io;
2
3macro_rules! read_bytes_ext {
4 ($output_type:ty) => {
5 impl<W: io::Read + ?Sized> ReadBytesExt<$output_type> for W {
6 #[inline]
7 fn read_be(&mut self) -> io::Result<$output_type> {
8 let mut bytes = [0u8; std::mem::size_of::<$output_type>()];
9 self.read_exact(&mut bytes)?;
10 Ok(<$output_type>::from_be_bytes(bytes))
11 }
12 }
13 };
14}
15
16macro_rules! write_bytes_ext {
17 ($input_type:ty) => {
18 impl<W: io::Write + ?Sized> WriteBytesExt<$input_type> for W {
19 #[inline]
20 fn write_be(&mut self, n: $input_type) -> io::Result<()> {
21 self.write_all(&n.to_be_bytes())
22 }
23 }
24 };
25}
26
27pub trait ReadBytesExt<T>: io::Read {
29 fn read_be(&mut self) -> io::Result<T>;
31}
32
33pub trait WriteBytesExt<T>: io::Write {
35 fn write_be(&mut self, _: T) -> io::Result<()>;
37}
38
39read_bytes_ext!(u8);
40read_bytes_ext!(u16);
41read_bytes_ext!(u32);
42
43write_bytes_ext!(u32);