3 releases
0.1.2 | May 12, 2024 |
---|---|
0.1.1 | May 4, 2024 |
0.1.0 | Apr 27, 2024 |
#2127 in Parser implementations
137 downloads per month
16KB
272 lines
rad crate for r/w
This crate provides neat ways to eat bytes out of your favorite readers and push bytes into cute writers.
This crate has three modules, one for each kind of endianness: be
(big endian), le
(little endian), and ne
(native endian— whatever your system is on)
Examples
Read unsigned 16 bit big-endian integers from a Reader
:
use raad::be::*; // < note how we specify we want big endian when we import the trait
let mut rdr = &mut &[02, 05, 03, 00][..];
assert_eq!([0x0205, 0x0300], rdr.r::<[u16; 2]>().unwrap());
Write unsigned 16 bit little-endian integers to a Writer
:
use raad::le::*; // and here we specify little endian
let mut wtr = vec![];
wtr.w([0x0205u16, 0x0300]).unwrap();
assert_eq!(wtr, vec![05, 02, 00, 03]);
Why
These helpers can greatly increase the ease of reading numbers and other things from a file/…
See, to read 3 u64s from a reader, you would have to go through all this trouble:
use std::io::Read;
fn read3(t: &mut impl Read) -> std::io::Result<[u64; 3]> {
let mut out = [0; 3];
let mut tmp = [0; 8];
for elem in &mut out {
t.read_exact(&mut tmp)?;
*elem = u64::from_ne_bytes(tmp);
}
Ok(out)
}
wheras, with this crate, its as simple as
use raad::ne::*;
t.read::<[u64; 3]>();