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

no-std byteorder_async

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

4 stable releases

1.2.0 Apr 25, 2020
1.1.0 Apr 24, 2020
1.0.1 Apr 22, 2020

#2255 in Encoding

Download history 23/week @ 2024-03-11 15/week @ 2024-03-18 17/week @ 2024-03-25 37/week @ 2024-04-01 10/week @ 2024-04-08 19/week @ 2024-04-15 19/week @ 2024-04-22 9/week @ 2024-04-29 9/week @ 2024-05-06 18/week @ 2024-05-13 14/week @ 2024-05-20 6/week @ 2024-05-27 16/week @ 2024-06-03 12/week @ 2024-06-10 8/week @ 2024-06-17 20/week @ 2024-06-24

56 downloads per month
Used in 5 crates (3 directly)

Unlicense OR MIT

225KB
3.5K SLoC

byteorder - async

This is a fork of byteorder with the addition of:

  • Using modern rust for try (? rather than try!)
  • support for tokio::io
  • support for futures::io

Installation

for futures::io

byteorder_async = {version="1.2.0", features=["futures_async"] }

for tokio::io

byteorder_async = {version="1.2.0", features=["tokio_async"] }

Basic async usage:

use byteorder_async::ReaderToByteOrder;

let reader : io::AsyncRead = ...;
// after the byte_order its the same calls.
let byte = reader.byte_order().read_u8().await;

Note: Thre reason for the byte_order() call is because async fn is not supprted in traits yet.


lib.rs:

This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order.

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::{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::{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 optionally provides support for 128 bit values (i128 and u128) when built with the i128 feature enabled.

This crate can also be used without the standard library.

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.

Dependencies

~0–1MB
~15K SLoC