#little-endian #endianness #big-endian #byte #binary

no-std byteorder-lite

Library for reading/writing numbers in big-endian and little-endian

1 unstable release

0.1.0 Apr 7, 2024

#779 in Encoding

Download history 80594/week @ 2024-07-22 121068/week @ 2024-07-29 113723/week @ 2024-08-05 169017/week @ 2024-08-12 167574/week @ 2024-08-19 122920/week @ 2024-08-26 138225/week @ 2024-09-02 141469/week @ 2024-09-09 145962/week @ 2024-09-16 160107/week @ 2024-09-23 165275/week @ 2024-09-30 169649/week @ 2024-10-07 174373/week @ 2024-10-14 180626/week @ 2024-10-21 198006/week @ 2024-10-28 200608/week @ 2024-11-04

765,284 downloads per month
Used in 1,441 crates (3 directly)

Unlicense OR MIT

145KB
2.5K SLoC

byteorder-lite

crates.io Documentation Build Status

This crate is a fork of the byteorder crate which sets #![forbid(unsafe_code)]. It includes all traits and most methods from the original crate, but the ReadBytesExt::read_*_into family of methods had to be removed because they currently cannot be implemented without unsafe code.

byteorder-lite is not affiliated with the main byteorder crate.


lib.rs:

This crate is a fork of the byteorder crate which sets #![forbid(unsafe_code)]. It includes all traits and most methods from the original crate, but the ReadBytesExt::read_*_into family of methods had to be removed because they currently cannot be implemented without unsafe code.

The organization of the crate is pretty simple. A trait, ByteOrder, specifies byte conversion methods for each type of number in Rust (sans numbers that have a platform dependent size like usize and isize). Two types, BigEndian and LittleEndian implement these methods. Finally, ReadBytesExt and WriteBytesExt provide convenience methods available to all types that implement Read and Write.

An alias, NetworkEndian, for BigEndian is provided to help improve code clarity.

An additional alias, NativeEndian, is provided for the endianness of the local platform. This is convenient when serializing data for use and conversions are not desired.

Examples

Read unsigned 16 bit big-endian integers from a Read type:

use std::io::Cursor;
use byteorder_lite::{BigEndian, ReadBytesExt};

let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
// Note that we use type parameters to indicate which kind of byte order
// we want!
assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());

Write unsigned 16 bit little-endian integers to a Write type:

use byteorder_lite::{LittleEndian, WriteBytesExt};

let mut wtr = vec![];
wtr.write_u16::<LittleEndian>(517).unwrap();
wtr.write_u16::<LittleEndian>(768).unwrap();
assert_eq!(wtr, vec![5, 2, 0, 3]);

Optional Features

This crate can also be used without the standard library.

Alternatives

The standard numeric types provide built-in methods like to_le_bytes and from_le_bytes, which support some of the same use cases.

No runtime deps