Struct bitflags::__core::io::BufWriter
[−]
[src]
pub struct BufWriter<W> where W: Write {
// some fields omitted
}
1.0.0Wraps a writer and buffers its output.
It can be excessively inefficient to work directly with something that
implements Write
. For example, every call to write
on TcpStream
results in a system call. A BufWriter
keeps an in-memory buffer of data
and writes it to an underlying writer in large, infrequent batches.
The buffer will be written out when the writer is dropped.
Examples
Let's write the numbers one through ten to a TcpStream
:
use std::io::prelude::*; use std::net::TcpStream; let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap(); for i in 1..10 { stream.write(&[i]).unwrap(); }
Because we're not buffering, we write each one in turn, incurring the
overhead of a system call per byte written. We can fix this with a
BufWriter
:
use std::io::prelude::*; use std::io::BufWriter; use std::net::TcpStream; let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); for i in 1..10 { stream.write(&[i]).unwrap(); }
By wrapping the stream with a BufWriter
, these ten writes are all grouped
together by the buffer, and will all be written out in one system call when
the stream
is dropped.