Struct io_streams::BufReaderLineWriter [−][src]
Wraps a reader and writer and buffers input and output to and from it, flushing
the writer whenever a newline (0x0a
, '\n'
) is detected on output.
The BufDuplexer
struct wraps a reader and writer and buffers their input and output.
But it only does this batched write when it goes out of scope, or when the
internal buffer is full. Sometimes, you'd prefer to write each line as it's
completed, rather than the entire buffer at once. Enter BufReaderLineWriter
. It
does exactly that.
Like BufDuplexer
, a BufReaderLineWriter
’s buffer will also be flushed when the
BufReaderLineWriter
goes out of scope or when its internal buffer is full.
If there's still a partial line in the buffer when the BufReaderLineWriter
is
dropped, it will flush those contents.
Examples
We can use BufReaderLineWriter
to write one line at a time, significantly
reducing the number of actual writes to the file.
use char_device::CharDevice; use io_streams::BufReaderLineWriter; use std::{fs, io::prelude::*}; fn main() -> std::io::Result<()> { let road_not_taken = b"I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I - I took the one less traveled by, And that has made all the difference."; let file = CharDevice::open("/dev/tty")?; let mut file = BufReaderLineWriter::new(file); file.write_all(b"I shall be telling this with a sigh")?; // No bytes are written until a newline is encountered (or // the internal buffer is filled). assert_eq!(fs::read_to_string("poem.txt")?, ""); file.write_all(b"\n")?; assert_eq!( fs::read_to_string("poem.txt")?, "I shall be telling this with a sigh\n", ); // Write the rest of the poem. file.write_all( b"Somewhere ages and ages hence: Two roads diverged in a wood, and I - I took the one less traveled by, And that has made all the difference.", )?; // The last line of the poem doesn't end in a newline, so // we have to flush or drop the `BufReaderLineWriter` to finish // writing. file.flush()?; // Confirm the whole poem was written. assert_eq!(fs::read("poem.txt")?, &road_not_taken[..]); Ok(()) }
Implementations
impl<Inner: HalfDuplex> BufReaderLineWriter<Inner>
[src]
pub fn new(inner: Inner) -> Self
[src]
Creates a new BufReaderLineWriter
.
Examples
use char_device::CharDevice; use io_streams::BufReaderLineWriter; fn main() -> std::io::Result<()> { let file = CharDevice::open("/dev/tty")?; let file = BufReaderLineWriter::new(file); Ok(()) }
pub fn with_capacities(
reader_capacity: usize,
writer_capacity: usize,
inner: Inner
) -> Self
[src]
reader_capacity: usize,
writer_capacity: usize,
inner: Inner
) -> Self
Creates a new BufReaderLineWriter
with a specified capacities for the internal
buffers.
Examples
use char_device::CharDevice; use io_streams::BufReaderLineWriter; fn main() -> std::io::Result<()> { let file = CharDevice::open("/dev/tty")?; let file = BufReaderLineWriter::with_capacities(10, 100, file); Ok(()) }
pub fn get_ref(&self) -> &Inner
[src]
Gets a reference to the underlying writer.
Examples
use char_device::CharDevice; use io_streams::BufReaderLineWriter; fn main() -> std::io::Result<()> { let file = CharDevice::open("/dev/tty")?; let file = BufReaderLineWriter::new(file); let reference = file.get_ref(); Ok(()) }
pub fn get_mut(&mut self) -> &mut Inner
[src]
Gets a mutable reference to the underlying writer.
Caution must be taken when calling methods on the mutable reference returned as extra writes could corrupt the output stream.
Examples
use char_device::CharDevice; use io_streams::BufReaderLineWriter; fn main() -> std::io::Result<()> { let file = CharDevice::open("/dev/tty")?; let mut file = BufReaderLineWriter::new(file); // we can use reference just like file let reference = file.get_mut(); Ok(()) }
pub fn into_inner(self) -> Result<Inner, IntoInnerError<Self>>
[src]
Unwraps this BufReaderLineWriter
, returning the underlying writer.
The internal buffer is written out before returning the writer.
Errors
An Err
will be returned if an error occurs while flushing the buffer.
Examples
use char_device::CharDevice; use io_streams::BufReaderLineWriter; fn main() -> std::io::Result<()> { let file = CharDevice::open("/dev/tty")?; let writer: BufReaderLineWriter<CharDevice> = BufReaderLineWriter::new(file); let file: CharDevice = writer.into_inner()?; Ok(()) }
Trait Implementations
impl<Inner: HalfDuplex + AsUnsafeHandle> AsUnsafeHandle for BufReaderLineWriter<Inner>
[src]
fn as_unsafe_handle(&self) -> UnsafeHandle
[src]
impl<Inner: HalfDuplex> BufRead for BufReaderLineWriter<Inner>
[src]
fn fill_buf(&mut self) -> Result<&[u8]>
[src]
fn consume(&mut self, amt: usize)
[src]
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>
[src]
fn read_line(&mut self, buf: &mut String) -> Result<usize>
[src]
pub fn split(self, byte: u8) -> Split<Self>
1.0.0[src]
pub fn lines(self) -> Lines<Self>
1.0.0[src]
impl<Inner: HalfDuplex> Debug for BufReaderLineWriter<Inner> where
Inner: Debug,
[src]
Inner: Debug,
impl<Inner: HalfDuplex> Read for BufReaderLineWriter<Inner>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
[src]
fn is_read_vectored(&self) -> bool
[src]
pub unsafe fn initializer(&self) -> Initializer
[src]
pub fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
pub fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
pub fn bytes(self) -> Bytes<Self>
1.0.0[src]
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
pub fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl<Inner: HalfDuplex> Write for BufReaderLineWriter<Inner>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn flush(&mut self) -> Result<()>
[src]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize>
[src]
fn is_write_vectored(&self) -> bool
[src]
fn write_all(&mut self, buf: &[u8]) -> Result<()>
[src]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
[src]
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>
[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<Inner> RefUnwindSafe for BufReaderLineWriter<Inner> where
Inner: RefUnwindSafe,
[src]
Inner: RefUnwindSafe,
impl<Inner> Send for BufReaderLineWriter<Inner> where
Inner: Send,
[src]
Inner: Send,
impl<Inner> Sync for BufReaderLineWriter<Inner> where
Inner: Sync,
[src]
Inner: Sync,
impl<Inner> Unpin for BufReaderLineWriter<Inner> where
Inner: Unpin,
[src]
Inner: Unpin,
impl<Inner> UnwindSafe for BufReaderLineWriter<Inner> where
Inner: UnwindSafe,
[src]
Inner: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,