[−][src]Function thread_io::write::writer
pub fn writer<W, F, O, E>(
bufsize: usize,
queuelen: usize,
writer: W,
func: F
) -> Result<O, E> where
F: FnOnce(&mut Writer) -> Result<O, E>,
W: Write + Send,
E: Send + From<Error>,
Sends writer
to a background thread and provides another writer in the
main thread, which then submits its data to the background writer.
The writer in the closure (func
) fills buffers of a given size (bufsize
)
and submits them to the background writer 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, or after writing is finished and the closure ends.
Also note that the last write()
in the background can happen after the
closure has ended. Finalizing actions should be done in the finish
closure
supplied to writer_finish
or
writer_init_finish
.
Example:
use thread_io::write::writer; use std::io::Write; let text = b"The quick brown fox jumps over the lazy dog"; let mut buf = vec![0; text.len()]; writer(16, 2, &mut buf[..], |writer| { writer.write_all(&text[..]) }).expect("write failed"); assert_eq!(&buf[..], &text[..]);