pub struct BufWriter<W: Write> { /* private fields */ }
Expand description
The BufWriter
adds buffering to any writer using a specialized buffer.
This is very similar std::io::BufWriter
, but it uses a Ring
for the
internal the buffer.
§Examples
use vmap::io::{BufReader, BufWriter};
let recv = TcpStream::connect(srv.local_addr().unwrap())?;
let send = /* accepted socked */
let mut wr = BufWriter::new(send, 4000).unwrap();
wr.write_all(b"hello\nworld\n")?;
wr.flush()?;
let mut rd = BufReader::new(recv, 4000).unwrap();
let mut line = String::new();
let len = rd.read_line(&mut line)?;
assert_eq!(line, "hello\n");
Implementations§
source§impl<W: Write> BufWriter<W>
impl<W: Write> BufWriter<W>
sourcepub fn from_parts(inner: W, buf: Ring) -> Self
pub fn from_parts(inner: W, buf: Ring) -> Self
Creates a new BufWriter
using an allocated, and possibly populated,
Ring
instance. Consider calling Ring::clear()
prior if the
contents of the ring should be discarded.
sourcepub fn into_inner(self) -> Result<W, IntoInnerError<W>>
pub fn into_inner(self) -> Result<W, IntoInnerError<W>>
Unwraps this BufWriter
, returning the underlying writer.
On Err
, the result is a tuple combining the error that occurred while
flusing the buffer, and the buffer object.
§Examples
use std::io::{self, Write, ErrorKind};
use vmap::io::BufWriter;
struct ErringWriter(usize);
impl Write for ErringWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// eventually fails with BrokenPipe
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
let mut stream = BufWriter::new(ErringWriter(6), 4096)?;
stream.write_all(b"hello\nworld\n")?;
// flush the buffer and get the original stream back
let stream = match stream.into_inner() {
Ok(s) => s,
Err(e) => {
assert_eq!(e.error().kind(), ErrorKind::BrokenPipe);
// You can forcefully obtain the stream, however it is in an
// failing state.
let (recovered_writer, ring) = e.into_inner().into_parts();
assert_eq!(ring.unwrap().as_ref(), b"world\n");
recovered_writer
}
};
sourcepub fn into_parts(self) -> (W, Result<Ring, WriterPanicked>)
pub fn into_parts(self) -> (W, Result<Ring, WriterPanicked>)
Disassembles this BufWriter
into the underlying writer and the Ring
used for buffering, containing any buffered but unwritted data.
If the underlying writer panicked during the previous write, the Ring
will be wrapped in a WriterPanicked
error. In this case, the content
still buffered within the Ring
may or may not have been written.
§Example
use std::io::{self, Write};
use std::panic::{catch_unwind, AssertUnwindSafe};
use vmap::io::BufWriter;
struct PanickingWriter;
impl Write for PanickingWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
fn flush(&mut self) -> io::Result<()> { panic!() }
}
let mut stream = BufWriter::new(PanickingWriter, 4096)?;
stream.write_all(b"testing")?;
let result = catch_unwind(AssertUnwindSafe(|| {
stream.flush().unwrap()
}));
assert!(result.is_err());
let (recovered_writer, ring) = stream.into_parts();
assert!(matches!(recovered_writer, PanickingWriter));
assert_eq!(ring.unwrap_err().into_inner().as_ref(), b"testing");
Trait Implementations§
source§impl<W: Write + Read> Read for BufWriter<W>
impl<W: Write + Read> Read for BufWriter<W>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read
, except that it reads into a slice of buffers. Read moresource§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf
. Read moresource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf
. Read moresource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf
. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)source§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moresource§impl<W: Write> Write for BufWriter<W>
impl<W: Write> Write for BufWriter<W>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)