Expand description
This crate provides convenience methods for encoding and decoding numbers in
either big-endian or little-endian order on top of asynchronous I/O streams.
It owes everything to the magnificent byteorder crate. This crate only
provides a shim to AsyncRead and AsyncWrite.
The organization of the crate mirrors that of byteorder. 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, AsyncReadBytesExt and
AsyncWriteBytesExt 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 an AsyncRead type:
use std::io::Cursor;
use tokio_byteorder::{BigEndian, AsyncReadBytesExt};
#[tokio::main]
async fn main() {
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>().await.unwrap());
assert_eq!(768, rdr.read_u16::<BigEndian>().await.unwrap());
}Write unsigned 16 bit little-endian integers to a AsyncWrite type:
use tokio_byteorder::{LittleEndian, AsyncWriteBytesExt};
#[tokio::main]
async fn main() {
let mut wtr = vec![];
wtr.write_u16::<LittleEndian>(517).await.unwrap();
wtr.write_u16::<LittleEndian>(768).await.unwrap();
assert_eq!(wtr, vec![5, 2, 0, 3]);
}§Alternatives
Note that as of Rust 1.32, the standard numeric types provide built-in methods
like to_le_bytes and from_le_bytes, which support some of the same use
cases.
Enums§
- BigEndian
- Defines big-endian serialization.
- Little
Endian - Defines little-endian serialization.
Traits§
- Async
Read Bytes Ext - Extends
AsyncReadwith methods for reading numbers. - Async
Write Bytes Ext - Extends
AsyncWritewith methods for writing numbers.
Type Aliases§
- Native
Endian - Defines system native-endian serialization.
- Network
Endian - Defines network byte order serialization.