Crate byteorder_lite

Source
Expand description

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.

Modulesยง

io ๐Ÿ”’
private ๐Ÿ”’

Macrosยง

read_slice ๐Ÿ”’
Copies a &u8 $src into a &mut [$ty] $dst for the endianness given by $from_bytes (must be either from_be_bytes or from_le_bytes).
write_slice ๐Ÿ”’
Copies a &[$ty] $src into a &mut u8 $dst for the endianness given by $from_bytes (must be either from_be_bytes or from_le_bytes).

Enumsยง

BigEndian
Defines big-endian serialization.
LittleEndian
Defines little-endian serialization.

Traitsยง

ByteOrder
ByteOrder describes types that can serialize integers as bytes.
ReadBytesExt
Extends Read with methods for reading numbers. (For std::io.)
WriteBytesExt
Extends Write with methods for writing numbers. (For std::io.)

Functionsยง

extend_sign ๐Ÿ”’
extend_sign128 ๐Ÿ”’
pack_size ๐Ÿ”’
pack_size128 ๐Ÿ”’
unextend_sign ๐Ÿ”’
unextend_sign128 ๐Ÿ”’

Type Aliasesยง

BE
A type alias for BigEndian.
LE
A type alias for LittleEndian.
NativeEndian
Defines system native-endian serialization.
NetworkEndian
Defines network byte order serialization.