Crates.io | bitendian |
lib.rs | bitendian |
version | 0.2.0 |
source | src |
created_at | 2023-11-07 00:45:23.366282 |
updated_at | 2023-11-07 01:13:09.688315 |
description | Ergonomic library for reading/writing numbers in big-endian and little-endian with async support. |
homepage | https://github.com/aatifsyed/bitendian |
repository | https://github.com/aatifsyed/bitendian |
max_upload_size | |
id | 1027391 |
size | 30,484 |
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);
byteorder
.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()?,
})
}
futures::io
and tokio::io
via the futures
and tokio
features respectively.u24
.#![no_std]
by disabling the default std
feature.