[][src]Crate tokio_byteorder

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.


Read unsigned 16 bit big-endian integers from an AsyncRead type:

use std::io::Cursor;
use tokio_byteorder::{BigEndian, AsyncReadBytesExt};

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};

async fn main() {
    let mut wtr = vec![];
    assert_eq!(wtr, vec![5, 2, 0, 3]);


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.



Defines big-endian serialization.


Defines little-endian serialization.



Extends AsyncRead with methods for reading numbers.


Extends AsyncWrite with methods for writing numbers.

Type Definitions


Defines system native-endian serialization.


Defines network byte order serialization.