AsyncReadBytesExt

Trait AsyncReadBytesExt 

Source
pub trait AsyncReadBytesExt: AsyncRead {
Show 16 methods // Provided methods fn read_u8<'a>(&'a mut self) -> ReadU8<&'a mut Self> where Self: Unpin { ... } fn read_i8<'a>(&'a mut self) -> ReadI8<&'a mut Self> where Self: Unpin { ... } fn read_u16<'a, T: ByteOrder>(&'a mut self) -> ReadU16<&'a mut Self, T> where Self: Unpin { ... } fn read_i16<'a, T: ByteOrder>(&'a mut self) -> ReadI16<&'a mut Self, T> where Self: Unpin { ... } fn read_u24<'a, T: ByteOrder>(&'a mut self) -> ReadU24<&'a mut Self, T> where Self: Unpin { ... } fn read_i24<'a, T: ByteOrder>(&'a mut self) -> ReadI24<&'a mut Self, T> where Self: Unpin { ... } fn read_u32<'a, T: ByteOrder>(&'a mut self) -> ReadU32<&'a mut Self, T> where Self: Unpin { ... } fn read_i32<'a, T: ByteOrder>(&'a mut self) -> ReadI32<&'a mut Self, T> where Self: Unpin { ... } fn read_u48<'a, T: ByteOrder>(&'a mut self) -> ReadU48<&'a mut Self, T> where Self: Unpin { ... } fn read_i48<'a, T: ByteOrder>(&'a mut self) -> ReadI48<&'a mut Self, T> where Self: Unpin { ... } fn read_u64<'a, T: ByteOrder>(&'a mut self) -> ReadU64<&'a mut Self, T> where Self: Unpin { ... } fn read_i64<'a, T: ByteOrder>(&'a mut self) -> ReadI64<&'a mut Self, T> where Self: Unpin { ... } fn read_u128<'a, T: ByteOrder>(&'a mut self) -> ReadU128<&'a mut Self, T> where Self: Unpin { ... } fn read_i128<'a, T: ByteOrder>(&'a mut self) -> ReadI128<&'a mut Self, T> where Self: Unpin { ... } fn read_f32<'a, T: ByteOrder>(&'a mut self) -> ReadF32<&'a mut Self, T> where Self: Unpin { ... } fn read_f64<'a, T: ByteOrder>(&'a mut self) -> ReadF64<&'a mut Self, T> where Self: Unpin { ... }
}
Expand description

Extends AsyncRead with methods for reading numbers.

Most of the methods defined here have an unconstrained type parameter that must be explicitly instantiated. Typically, it is instantiated with either the BigEndian or LittleEndian types defined in this crate.

§Examples

Read unsigned 16 bit big-endian integers from a [Read]:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
    assert_eq!(517, rdr.read_u16::<BigEndian>().await.unwrap());
    assert_eq!(768, rdr.read_u16::<BigEndian>().await.unwrap());
}

Provided Methods§

Source

fn read_u8<'a>(&'a mut self) -> ReadU8<&'a mut Self>
where Self: Unpin,

Reads an unsigned 8 bit integer from the underlying reader.

Note that since this reads a single byte, no byte order conversions are used. It is included for completeness.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read unsigned 8 bit integers from a Read:

use std::io::Cursor;
use tokio_byteorder::AsyncReadBytesExt;

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![2, 5]);
    assert_eq!(2, rdr.read_u8().await.unwrap());
    assert_eq!(5, rdr.read_u8().await.unwrap());
}
Source

fn read_i8<'a>(&'a mut self) -> ReadI8<&'a mut Self>
where Self: Unpin,

Reads a signed 8 bit integer from the underlying reader.

Note that since this reads a single byte, no byte order conversions are used. It is included for completeness.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read signed 8 bit integers from a Read:

use std::io::Cursor;
use tokio_byteorder::AsyncReadBytesExt;

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x02, 0xfb]);
    assert_eq!(2, rdr.read_i8().await.unwrap());
    assert_eq!(-5, rdr.read_i8().await.unwrap());
}
Source

fn read_u16<'a, T: ByteOrder>(&'a mut self) -> ReadU16<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 16 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

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

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
    assert_eq!(517, rdr.read_u16::<BigEndian>().await.unwrap());
    assert_eq!(768, rdr.read_u16::<BigEndian>().await.unwrap());
}
Source

fn read_i16<'a, T: ByteOrder>(&'a mut self) -> ReadI16<&'a mut Self, T>
where Self: Unpin,

Reads a signed 16 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read signed 16 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]);
    assert_eq!(193, rdr.read_i16::<BigEndian>().await.unwrap());
    assert_eq!(-132, rdr.read_i16::<BigEndian>().await.unwrap());
}
Source

fn read_u24<'a, T: ByteOrder>(&'a mut self) -> ReadU24<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 24 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read unsigned 24 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x00, 0x01, 0x0b]);
    assert_eq!(267, rdr.read_u24::<BigEndian>().await.unwrap());
}
Source

fn read_i24<'a, T: ByteOrder>(&'a mut self) -> ReadI24<&'a mut Self, T>
where Self: Unpin,

Reads a signed 24 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read signed 24 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0xff, 0x7a, 0x33]);
    assert_eq!(-34253, rdr.read_i24::<BigEndian>().await.unwrap());
}
Source

fn read_u32<'a, T: ByteOrder>(&'a mut self) -> ReadU32<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 32 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read unsigned 32 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x00, 0x00, 0x01, 0x0b]);
    assert_eq!(267, rdr.read_u32::<BigEndian>().await.unwrap());
}
Source

fn read_i32<'a, T: ByteOrder>(&'a mut self) -> ReadI32<&'a mut Self, T>
where Self: Unpin,

Reads a signed 32 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read signed 32 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0xff, 0xff, 0x7a, 0x33]);
    assert_eq!(-34253, rdr.read_i32::<BigEndian>().await.unwrap());
}
Source

fn read_u48<'a, T: ByteOrder>(&'a mut self) -> ReadU48<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 48 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read unsigned 48 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0xb6, 0x71, 0x6b, 0xdc, 0x2b, 0x31]);
    assert_eq!(200598257150769, rdr.read_u48::<BigEndian>().await.unwrap());
}
Source

fn read_i48<'a, T: ByteOrder>(&'a mut self) -> ReadI48<&'a mut Self, T>
where Self: Unpin,

Reads a signed 48 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read signed 48 bit big-endian integers from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x9d, 0x71, 0xab, 0xe7, 0x97, 0x8f]);
    assert_eq!(-108363435763825, rdr.read_i48::<BigEndian>().await.unwrap());
}
Source

fn read_u64<'a, T: ByteOrder>(&'a mut self) -> ReadU64<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 64 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read an unsigned 64 bit big-endian integer from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83]);
    assert_eq!(918733457491587, rdr.read_u64::<BigEndian>().await.unwrap());
}
Source

fn read_i64<'a, T: ByteOrder>(&'a mut self) -> ReadI64<&'a mut Self, T>
where Self: Unpin,

Reads a signed 64 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read a signed 64 bit big-endian integer from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0]);
    assert_eq!(i64::min_value(), rdr.read_i64::<BigEndian>().await.unwrap());
}
Source

fn read_u128<'a, T: ByteOrder>(&'a mut self) -> ReadU128<&'a mut Self, T>
where Self: Unpin,

Reads an unsigned 128 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read an unsigned 128 bit big-endian integer from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![
        0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
        0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
    ]);
    assert_eq!(16947640962301618749969007319746179, rdr.read_u128::<BigEndian>().await.unwrap());
}
Source

fn read_i128<'a, T: ByteOrder>(&'a mut self) -> ReadI128<&'a mut Self, T>
where Self: Unpin,

Reads a signed 128 bit integer from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read a signed 128 bit big-endian integer from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
    assert_eq!(i128::min_value(), rdr.read_i128::<BigEndian>().await.unwrap());
}
Source

fn read_f32<'a, T: ByteOrder>(&'a mut self) -> ReadF32<&'a mut Self, T>
where Self: Unpin,

Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read a big-endian single-precision floating point number from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![
        0x40, 0x49, 0x0f, 0xdb,
    ]);
    assert_eq!(f32::consts::PI, rdr.read_f32::<BigEndian>().await.unwrap());
}
Source

fn read_f64<'a, T: ByteOrder>(&'a mut self) -> ReadF64<&'a mut Self, T>
where Self: Unpin,

Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader.

§Errors

This method returns the same errors as Read::read_exact.

§Examples

Read a big-endian double-precision floating point number from a Read:

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

#[tokio::main]
async fn main() {
    let mut rdr = Cursor::new(vec![
        0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
    ]);
    assert_eq!(f64::consts::PI, rdr.read_f64::<BigEndian>().await.unwrap());
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<R: AsyncRead + ?Sized> AsyncReadBytesExt for R

All types that implement AsyncRead get methods defined in AsyncReadBytesExt for free.