eio
Read and write numbers in big-endian and little-endian.
🚀 Getting started
Add the following to your Cargo manifest.
[]
= "0.1"
And bring the ReadExt and/or WriteExt traits into scope.
use ;
🤸 Usage
The most common usage is parsing numbers from a source. You can do this using
the read_le() and read_be() methods on anything that implements Read.
use ReadExt;
// `Cursor` implements `Read`
let mut rdr = new;
// Read a two byte `u16` in little-endian order
let i: u16 = rdr.read_le?;
assert_eq!;
// Read a four byte `i32` in big-endian order
let i: i32 = rdr.read_be?;
assert_eq!;
// Read a three byte array
let a: = rdr.read_array?;
assert_eq!;
Serialization of numbers can be done using the write_le() and write_be().
This can be done on anything that implements Write.
use WriteExt;
// `&mut [u8]` implements `Write`.
let mut wtr = Vecnew;
// Write a four byte `f32` in little-endian order
wtr.write_le?;
// Write a one byte `u8`
wtr.write_be?;
assert_eq!;
In no_std contexts the FromBytes and ToBytes traits can be used directly.
use ;
let x: u32 = from_be_bytes;
assert_eq!;
let data = to_le_bytes;
assert_eq!;
💡 Prior art
eio provides the same capabilities as the popular byteorder crate but with
a very different API. The advantages of eio are the following:
- It is extendible, anyone can implement
FromBytesorToBytesfor their own integer types. - Uses the core/std
{from,to}_{le,be}_bytesfunctions to do the conversion for floats and integers.byteorderreimplements these. - Doesn't require turbofish type annotations all the time.
// byteorder let i = rdr.?; // eio let i: u16 = rdr.read_be?;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.