#endian #binary #async #tokio #futures

no-std bitendian

Ergonomic library for reading/writing numbers in big-endian and little-endian with async support

2 unstable releases

0.2.0 Nov 7, 2023
0.1.0 Nov 7, 2023

#1124 in Encoding

Download history 38/week @ 2023-11-04 1/week @ 2023-11-11 9/week @ 2023-11-18 8/week @ 2023-11-25 2/week @ 2023-12-09 4/week @ 2023-12-23 2/week @ 2023-12-30 8/week @ 2024-01-27 12/week @ 2024-02-10 44/week @ 2024-02-17

64 downloads per month

MIT/Apache

31KB
518 lines

Convenience methods for encoding and decoding numbers in either big-endian or little-endian.

Primitive integers implement BitEndian.

use bitendian::BitEndian;

let it: u16 = 256;
assert_eq!(BitEndian::to_be_bytes(it), [1, 0]);
assert_eq!(BitEndian::to_le_bytes(it), [0, 1]);

Extension methods provide convenient readers and writers.

use bitendian::{io::WriteExt as _, tokio::AsyncReadExt as _};

let mut buf = vec![];
buf.write_be(1u16)?;
let swapped = buf.as_slice().read_le().await?;
assert_eq!(256u16, swapped);

Comparison with byteorder.

  • This crate leverages type inference to avoid defining dozens of e.g write_uXX methods.
    use byteorder::{ReadBytesExt as _, BE, LE};
    use bitendian::io::ReadExt as _;
    use std::io;
    
    fn read_header(mut r: impl io::Read) -> io::Result<Header> {
        // before...
        Ok(Header {
            count: r.read_u16::<BE>()?,
                       // ^ this can be inferred
            offset: r.read_i32::<LE>()?
                              // ^ this could be a plain method
        })
        // after
        Ok(Header {
            count: r.read_be()?,
            offset: r.read_le()?,
        })
    }
    
  • This crate supports run-time endianness.
  • This crate supports futures::io and tokio::io via the futures and tokio features respectively.
  • This crate only supports rust's built-in types, not, eg. u24.
  • Both crates support #![no_std] by disabling the default std feature.

Dependencies

~0–1.2MB
~22K SLoC