| Crates.io | raad |
| lib.rs | raad |
| version | 0.1.2 |
| created_at | 2024-04-27 07:38:17.793142+00 |
| updated_at | 2024-05-12 09:55:17.103689+00 |
| description | raad library for reading and writing bytes |
| homepage | |
| repository | https://github.com/bend-n/raad |
| max_upload_size | |
| id | 1222392 |
| size | 16,980 |
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)
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]);
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]>();