Function fastq::thread_reader[][src]

pub fn thread_reader<R, F, O>(
    bufsize: usize,
    queuelen: usize,
    reader: R,
    func: F
) -> Result<O> where
    F: FnOnce(&mut ThreadReader) -> O,
    R: Read + Send + 'static, 

Wrap a reader in a background thread.

This is only useful for readers that do expensive operations (eg decompression).

The thread precomputes queuelen many reads with the given buffer size, and then waits for the consumer to catch up.

If the reader panics, reads to the consumer in the main thread returns ErrorKind::BrokenPipe errors and the return value of thread_reader contains the panic from the reader thread.

Examples

extern crate lz4;
extern crate fastq;

use std::io::stdin;
use fastq::thread_reader;

// lz4 is faster with a buffer size equal to the block size (default 4MB)
const BUFSIZE: usize = 1 << 22;
// The number of buffers the background thread fills, before it waits for
// a consumer to catch up.
const QUEUELEN: usize = 2;

let file = lz4::Decoder::new(stdin()).unwrap();
let out = thread_reader(BUFSIZE, QUEUELEN, file, |reader| {
    // do something with the reader
});