[−][src]Function thread_io::read::reader
pub fn reader<R, F, O, E>(
bufsize: usize,
queuelen: usize,
reader: R,
func: F
) -> Result<O, E> where
F: FnOnce(&mut Reader) -> Result<O, E>,
R: Read + Send,
E: Send,
Sends reader
to a background thread and provides a reader in the main
thread, which obtains data from the background reader.
The background reader fills buffers of a given size (bufsize
) and submits
them to the main thread through a channel. The queue length of the channel
can be configured using the queuelen
parameter (must be ≥ 1). As a
consequence, errors will not be returned immediately, but after some delay.
The reader in the background thread will stop if an error occurs, except for
errors of kind io::ErrorKind::Interrupted
. In this case, reading continues
in the background, but the error is still returned.
Example:
use thread_io::read::reader; use std::io::Read; let text = b"The quick brown fox jumps over the lazy dog"; let mut target = vec![]; reader(16, 2, &text[..], |rdr| { rdr.read_to_end(&mut target) }).expect("read failed"); assert_eq!(target.as_slice(), &text[..]);