[][src]Struct byteio::Reader

pub struct Reader<'a, R: ReadBytes<'a>>(_, _, _);

A convenience structure used for counting the number of bytes read.

This structure wraps an implementation of ReadBytes. It forwards read operations to the inner type while also maintaining a count of the number of bytes that pass through it.

When you have finished with it, you can return the original type via the into_inner method.

Examples

use byteio::{ReadBytes, ReadBytesExt, Reader};
use byteorder::BigEndian;

fn main() -> byteio::Result<()> {
    let buf: &[u8] = &[1, 0, 2, 0, 0, 0, 3];

    let mut reader = Reader::new(buf);

    assert_eq!(reader.try_read_u8()?, 1);
    assert_eq!(reader.try_read_u16::<BigEndian>()?, 2);
    assert_eq!(reader.try_read_u32::<BigEndian>()?, 3);

    assert_eq!(reader.num_bytes_read(), 7);

    let inner = reader.into_inner();
    assert!(inner.is_empty());

    Ok(())
}

Methods

impl<'a, R: ReadBytes<'a>> Reader<'a, R>[src]

pub fn new(reader: R) -> Self[src]

Creates a new Reader by wrapping a ReadBytes implementor.

Examples

use byteio::Reader;

let buf = [0_u8; 2];
let mut reader = Reader::new(&buf[..]);

pub fn num_bytes_read(&self) -> usize[src]

Retrieves the number of bytes that have been read by this Reader.

Examples

use byteio::{ReadBytes, Reader};

let buf = [0_u8; 2];
let mut reader = Reader::new(&buf[..]);
let _ = reader.read_exact(1);
let _ = reader.read_exact(1);

assert_eq!(reader.num_bytes_read(), 2);

pub fn into_inner(self) -> R[src]

Consumes this Reader and returns the original ReadBytes implementor.

Examples

use byteio::{ReadBytes, Reader};

let buf = [0_u8; 2];
let mut reader = Reader::new(&buf[..]);
let _ = reader.read_exact(1);
let inner = reader.into_inner();

assert_eq!(inner.len(), 1); // the reader consumed one byte from its view of the slice

Trait Implementations

impl<'a, R: ReadBytes<'a>> ReadBytes<'a> for Reader<'a, R>[src]

fn try_read_exact(&mut self, n: usize) -> Result<&'a [u8]>[src]

Attempts to read exactly n bytes from the buffer. Read more

impl<'a, R: Debug + ReadBytes<'a>> Debug for Reader<'a, R>[src]

impl<'a, R: PartialEq + ReadBytes<'a>> PartialEq<Reader<'a, R>> for Reader<'a, R>[src]

impl<'a, R: Eq + ReadBytes<'a>> Eq for Reader<'a, R>[src]

impl<'a, R: ReadBytes<'a>> AsRef<[u8]> for Reader<'a, R>[src]

impl<'a, R: Clone + ReadBytes<'a>> Clone for Reader<'a, R>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl<'a, R> Send for Reader<'a, R> where
    R: Send

impl<'a, R> Sync for Reader<'a, R> where
    R: Sync

Blanket Implementations

impl<'a, R> ReadBytesExt<'a> for R where
    R: ReadBytes<'a>, 
[src]

fn read_u8(&mut self) -> u8[src]

Reads a u8 from the underlying buffer. Read more

fn try_read_u8(&mut self) -> Result<u8>[src]

Attempts to read a u8 from the underlying buffer. Read more

fn read_i8(&mut self) -> i8[src]

Reads an i8 from the underlying buffer. Read more

fn try_read_i8(&mut self) -> Result<i8>[src]

Attempts to read an i8 from the underlying buffer. Read more

fn read_u16<E: ByteOrder>(&mut self) -> u16[src]

Reads a u16 from the underlying buffer. Read more

fn try_read_u16<E: ByteOrder>(&mut self) -> Result<u16>[src]

Attempts to read a u16 from the underlying buffer. Read more

fn read_i16<E: ByteOrder>(&mut self) -> i16[src]

Reads an i16 from the underlying buffer. Read more

fn try_read_i16<E: ByteOrder>(&mut self) -> Result<i16>[src]

Attempts to read an i16 from the underlying buffer. Read more

fn read_u32<E: ByteOrder>(&mut self) -> u32[src]

Reads a u32 from the underlying buffer. Read more

fn try_read_u32<E: ByteOrder>(&mut self) -> Result<u32>[src]

Attempts to read a u32 from the underlying buffer. Read more

fn read_i32<E: ByteOrder>(&mut self) -> i32[src]

Reads an i32 from the underlying buffer. Read more

fn try_read_i32<E: ByteOrder>(&mut self) -> Result<i32>[src]

Attempts to read an i32 from the underlying buffer. Read more

fn read_u64<E: ByteOrder>(&mut self) -> u64[src]

Reads a u64 from the underlying buffer. Read more

fn try_read_u64<E: ByteOrder>(&mut self) -> Result<u64>[src]

Attempts to read a u64 from the underlying buffer. Read more

fn read_i64<E: ByteOrder>(&mut self) -> i64[src]

Reads an i64 from the underlying buffer. Read more

fn try_read_i64<E: ByteOrder>(&mut self) -> Result<i64>[src]

Attempts to read an i64 from the underlying buffer. Read more

fn read_u128<E: ByteOrder>(&mut self) -> u128[src]

Reads a u128 from the underlying buffer. Read more

fn try_read_u128<E: ByteOrder>(&mut self) -> Result<u128>[src]

Attempts to read a u128 from the underlying buffer. Read more

fn read_i128<E: ByteOrder>(&mut self) -> i128[src]

Reads an i128 from the underlying buffer. Read more

fn try_read_i128<E: ByteOrder>(&mut self) -> Result<i128>[src]

Attempts to read an i128 from the underlying buffer. Read more

fn read_f32<E: ByteOrder>(&mut self) -> f32[src]

Reads an IEEE754 f32 from the underlying buffer. Read more

fn try_read_f32<E: ByteOrder>(&mut self) -> Result<f32>[src]

Attempts to read an IEEE754 f32 from the underlying buffer. Read more

fn read_f64<E: ByteOrder>(&mut self) -> f64[src]

Reads an IEEE754 f64 from the underlying buffer. Read more

fn try_read_f64<E: ByteOrder>(&mut self) -> Result<f64>[src]

Attempts to read an IEEE754 f64 from the underlying buffer. Read more

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.