[][src]Struct noodle::ReadMuncher

pub struct ReadMuncher<'a, T, F> where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>, 
{ /* fields omitted */ }

Continuously reads bytes from a Read implementor, parses that byte stream with the provided parser function, and provides an iterator over the parsed slices/packets/frames.

use std::io::Cursor;

let mut read = Cursor::new(vec![0xff, 1, 0xff, 2, 2, 0xff, 3, 3, 3]);

let munched: Vec<Vec<u8>> = ReadMuncher::new(&mut read, 5, |b, _| {
    if b.len() >= 2 {
        let skip = b[1] as usize + 1;
        if b.len() > skip {
            let blob = &b[..skip];
            Ok(Some((blob.to_owned(), skip)))
        } else {
            Ok(None)
        }
    } else {
        Ok(None)
    }
})
.collect();

assert_eq!(munched.len(), 3);
assert_eq!(munched[0], vec![0xff, 1]);
assert_eq!(munched[1], vec![0xff, 2, 2]);
assert_eq!(munched[2], vec![0xff, 3, 3, 3]);

Methods

impl<'a, T, F> ReadMuncher<'a, T, F> where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>, 
[src]

pub fn new(reader: &'a mut dyn Read, alloc_size: usize, parse_fn: F) -> Self[src]

Starts a new ReadMuncher instance.

reader

The Read implementor to be read for bytes.

alloc_size

Internal buffer allocation increment size.

This sets the initial buffer size, and size increase increments when necessary.

parse_fn

The parse function called against the read buffer. This can be a static function or a closure, with signature Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>.

The first parameter is a reference to the unconsumed slice of the read buffer. The second parameter is a boolean, which signals EOF (no more bytes available to read).

The return type is used internally to consume/step forward. Below is some example return values and what they do:

This example is not tested

Ok(Some((item, 12)))  // 'item' is returned, buffer drains 12 bytes

Ok(None)  // Not enough bytes, keep trying

Err(...)  // Error occurred. Iterator panics and prints to stderr

Trait Implementations

impl<'a, T, F> Iterator for ReadMuncher<'a, T, F> where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>, 
[src]

type Item = T

The type of the elements being iterated over.

Auto Trait Implementations

impl<'a, T, F> !RefUnwindSafe for ReadMuncher<'a, T, F>

impl<'a, T, F> !Send for ReadMuncher<'a, T, F>

impl<'a, T, F> !Sync for ReadMuncher<'a, T, F>

impl<'a, T, F> Unpin for ReadMuncher<'a, T, F> where
    F: Unpin

impl<'a, T, F> !UnwindSafe for ReadMuncher<'a, T, F>

Blanket Implementations

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

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

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

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

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

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

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> 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.