pub trait FromBitStream {
    type Error;

    // Required method
    fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error>
       where Self: Sized;
}
Expand description

Implemented by complex types that don’t require any additional context to parse themselves from a reader. Analagous to FromStr.

Example

use std::io::{Cursor, Read};
use bitstream_io::{BigEndian, BitRead, BitReader, FromBitStream};

#[derive(Debug, PartialEq, Eq)]
struct BlockHeader {
    last_block: bool,
    block_type: u8,
    block_size: u32,
}

impl FromBitStream for BlockHeader {
    type Error = std::io::Error;

    fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> std::io::Result<Self> {
        Ok(Self {
            last_block: r.read_bit()?,
            block_type: r.read(7)?,
            block_size: r.read(24)?,
        })
    }
}

let mut reader = BitReader::endian(Cursor::new(b"\x04\x00\x00\x7A"), BigEndian);
assert_eq!(
    reader.parse::<BlockHeader>().unwrap(),
    BlockHeader { last_block: false, block_type: 4, block_size: 122 }
);

Required Associated Types§

source

type Error

Error generated during parsing, such as io::Error

Required Methods§

source

fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error>where Self: Sized,

Parse Self from reader

Implementors§