bitstream_io::read

Trait ByteRead

Source
pub trait ByteRead {
    // Required methods
    fn read<V>(&mut self) -> Result<V, Error>
       where V: Primitive;
    fn read_as<F, V>(&mut self) -> Result<V, Error>
       where F: Endianness,
             V: Primitive;
    fn skip(&mut self, bytes: u32) -> Result<()>;
    fn reader_ref(&mut self) -> &mut dyn Read;

    // Provided methods
    fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()> { ... }
    fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]> { ... }
    fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>> { ... }
    fn parse<F: FromByteStream>(&mut self) -> Result<F, F::Error> { ... }
    fn parse_with<'a, F: FromByteStreamWith<'a>>(
        &mut self,
        context: &F::Context,
    ) -> Result<F, F::Error> { ... }
}
Expand description

A trait for anything that can read aligned values from an input stream

Required Methods§

Source

fn read<V>(&mut self) -> Result<V, Error>
where V: Primitive,

Reads whole numeric value from stream

§Errors

Passes along any I/O error from the underlying stream.

§Examples
use std::io::{Read, Cursor};
use bitstream_io::{BigEndian, ByteReader, ByteRead};
let data = [0b00000000, 0b11111111];
let mut reader = ByteReader::endian(Cursor::new(&data), BigEndian);
assert_eq!(reader.read::<u16>().unwrap(), 0b0000000011111111);
use std::io::{Read, Cursor};
use bitstream_io::{LittleEndian, ByteReader, ByteRead};
let data = [0b00000000, 0b11111111];
let mut reader = ByteReader::endian(Cursor::new(&data), LittleEndian);
assert_eq!(reader.read::<u16>().unwrap(), 0b1111111100000000);
Source

fn read_as<F, V>(&mut self) -> Result<V, Error>
where F: Endianness, V: Primitive,

Reads whole numeric value from stream in a potentially different endianness

§Errors

Passes along any I/O error from the underlying stream.

§Examples
use std::io::{Read, Cursor};
use bitstream_io::{BigEndian, ByteReader, ByteRead, LittleEndian};
let data = [0b00000000, 0b11111111];
let mut reader = ByteReader::endian(Cursor::new(&data), BigEndian);
assert_eq!(reader.read_as::<LittleEndian, u16>().unwrap(), 0b1111111100000000);
use std::io::{Read, Cursor};
use bitstream_io::{BigEndian, ByteReader, ByteRead, LittleEndian};
let data = [0b00000000, 0b11111111];
let mut reader = ByteReader::endian(Cursor::new(&data), LittleEndian);
assert_eq!(reader.read_as::<BigEndian, u16>().unwrap(), 0b0000000011111111);
Source

fn skip(&mut self, bytes: u32) -> Result<()>

Skips the given number of bytes in the stream.

§Errors

Passes along any I/O error from the underlying stream.

Source

fn reader_ref(&mut self) -> &mut dyn Read

Returns mutable reference to underlying reader

Provided Methods§

Source

fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()>

Completely fills the given buffer with whole bytes.

§Errors

Passes along any I/O error from the underlying stream.

Source

fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>

👎Deprecated since 1.8.0: use read() method instead

Completely fills a whole buffer with bytes and returns it.

§Errors

Passes along any I/O error from the underlying stream.

Source

fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>>

Completely fills a vector of bytes and returns it.

§Errors

Passes along any I/O error from the underlying stream.

Source

fn parse<F: FromByteStream>(&mut self) -> Result<F, F::Error>

Parses and returns complex type

Source

fn parse_with<'a, F: FromByteStreamWith<'a>>( &mut self, context: &F::Context, ) -> Result<F, F::Error>

Parses and returns complex type with context

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§