Struct io_streams::BufReaderLineWriter
source · pub struct BufReaderLineWriter<Inner: HalfDuplex> { /* private fields */ }
Expand description
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;
use std::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§
source§impl<Inner: HalfDuplex> BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex> BufReaderLineWriter<Inner>
sourcepub fn new(inner: Inner) -> Self
pub fn new(inner: Inner) -> Self
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(())
}
sourcepub fn with_capacities(
reader_capacity: usize,
writer_capacity: usize,
inner: Inner,
) -> Self
pub fn with_capacities( 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(())
}
sourcepub fn get_ref(&self) -> &Inner
pub fn get_ref(&self) -> &Inner
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(())
}
sourcepub fn get_mut(&mut self) -> &mut Inner
pub fn get_mut(&mut self) -> &mut Inner
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(())
}
sourcepub fn into_inner(self) -> Result<Inner, IntoInnerError<Self>>
pub fn into_inner(self) -> Result<Inner, IntoInnerError<Self>>
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§
source§impl<Inner: HalfDuplex AsFd> AsFd for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex AsFd> AsFd for BufReaderLineWriter<Inner>
source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
source§impl<Inner: HalfDuplex AsRawFd> AsRawFd for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex AsRawFd> AsRawFd for BufReaderLineWriter<Inner>
source§impl<Inner: HalfDuplex> BufRead for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex> BufRead for BufReaderLineWriter<Inner>
source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moresource§fn read_line(&mut self, buf: &mut String) -> Result<usize>
fn read_line(&mut self, buf: &mut String) -> Result<usize>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moresource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read moresource§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
bufread_skip_until
)byte
or EOF is reached. Read moresource§impl<Inner> Debug for BufReaderLineWriter<Inner>where
Inner: Debug HalfDuplex,
impl<Inner> Debug for BufReaderLineWriter<Inner>where
Inner: Debug HalfDuplex,
source§impl<Inner: HalfDuplex> Read for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex> Read for BufReaderLineWriter<Inner>
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 is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§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<Inner: HalfDuplex> Write for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex> Write for BufReaderLineWriter<Inner>
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
)source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
write_all_vectored
)Auto Trait Implementations§
impl<Inner> Freeze for BufReaderLineWriter<Inner>where
Inner: Freeze,
impl<Inner> RefUnwindSafe for BufReaderLineWriter<Inner>where
Inner: RefUnwindSafe,
impl<Inner> Send for BufReaderLineWriter<Inner>where
Inner: Send,
impl<Inner> Sync for BufReaderLineWriter<Inner>where
Inner: Sync,
impl<Inner> Unpin for BufReaderLineWriter<Inner>where
Inner: Unpin,
impl<Inner> UnwindSafe for BufReaderLineWriter<Inner>where
Inner: UnwindSafe,
Blanket Implementations§
source§impl<T> AsFilelike for Twhere
T: AsFd,
impl<T> AsFilelike for Twhere
T: AsFd,
source§fn as_filelike(&self) -> BorrowedFd<'_>
fn as_filelike(&self) -> BorrowedFd<'_>
source§fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>where
Target: FilelikeViewType,
fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>where
Target: FilelikeViewType,
&Target
. Read moresource§impl<T> AsGrip for Twhere
T: AsFd,
impl<T> AsGrip for Twhere
T: AsFd,
source§fn as_grip(&self) -> BorrowedFd<'_>
fn as_grip(&self) -> BorrowedFd<'_>
source§impl<T> AsRawFilelike for Twhere
T: AsRawFd,
impl<T> AsRawFilelike for Twhere
T: AsRawFd,
source§fn as_raw_filelike(&self) -> i32
fn as_raw_filelike(&self) -> i32
source§impl<T> AsRawGrip for Twhere
T: AsRawFd,
impl<T> AsRawGrip for Twhere
T: AsRawFd,
source§fn as_raw_grip(&self) -> i32
fn as_raw_grip(&self) -> i32
source§impl<T> AsRawSocketlike for Twhere
T: AsRawFd,
impl<T> AsRawSocketlike for Twhere
T: AsRawFd,
source§fn as_raw_socketlike(&self) -> i32
fn as_raw_socketlike(&self) -> i32
source§impl<T> AsSocketlike for Twhere
T: AsFd,
impl<T> AsSocketlike for Twhere
T: AsFd,
source§fn as_socketlike(&self) -> BorrowedFd<'_>
fn as_socketlike(&self) -> BorrowedFd<'_>
source§fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where
Target: SocketlikeViewType,
fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where
Target: SocketlikeViewType,
&Target
. Read moresource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FileIoExt for Twhere
T: AsFilelike IoExt,
impl<T> FileIoExt for Twhere
T: AsFilelike IoExt,
source§fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error>
fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error>
source§fn allocate(&self, offset: u64, len: u64) -> Result<(), Error>
fn allocate(&self, offset: u64, len: u64) -> Result<(), Error>
source§fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize, Error>
fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize, Error>
source§fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error>
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error>
source§fn read_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64,
) -> Result<usize, Error>
fn read_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset: u64, ) -> Result<usize, Error>
read_vectored
what read_at
is to read
.source§fn is_read_vectored_at(&self) -> bool
fn is_read_vectored_at(&self) -> bool
FileIoExt
implementation has an efficient
read_vectored_at
implementation.source§fn read_to_end_at(&self, buf: &mut Vec<u8>, offset: u64) -> Result<usize, Error>
fn read_to_end_at(&self, buf: &mut Vec<u8>, offset: u64) -> Result<usize, Error>
offset
, until EOF in this source, placing
them into buf
.source§fn read_to_string_at(
&self,
buf: &mut String,
offset: u64,
) -> Result<usize, Error>
fn read_to_string_at( &self, buf: &mut String, offset: u64, ) -> Result<usize, Error>
offset
, until EOF in this source,
appending them to buf
.source§fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize, Error>
fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize, Error>
source§fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<(), Error>
fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<(), Error>
source§fn write_vectored_at(
&self,
bufs: &[IoSlice<'_>],
offset: u64,
) -> Result<usize, Error>
fn write_vectored_at( &self, bufs: &[IoSlice<'_>], offset: u64, ) -> Result<usize, Error>
write_vectored
what write_at
is to write
.source§fn is_write_vectored_at(&self) -> bool
fn is_write_vectored_at(&self) -> bool
FileIoExt
implementation has an efficient
write_vectored_at
implementation.source§fn append(&self, buf: &[u8]) -> Result<usize, Error>
fn append(&self, buf: &[u8]) -> Result<usize, Error>
source§fn append_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
fn append_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
append
what write_vectored
is to write
.source§fn is_append_vectored(&self) -> bool
fn is_append_vectored(&self) -> bool
FileIoExt
implementation has an efficient
append_vectored
implementation.source§fn seek(&self, pos: SeekFrom) -> Result<u64, Error>
fn seek(&self, pos: SeekFrom) -> Result<u64, Error>
source§fn stream_position(&self) -> Result<u64, Error>
fn stream_position(&self) -> Result<u64, Error>
source§fn read_exact_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64,
) -> Result<(), Error>
fn read_exact_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset: u64, ) -> Result<(), Error>
read_exact_vectored
what read_exact_at
is to read_exact
.source§fn write_all_vectored_at(
&self,
bufs: &mut [IoSlice<'_>],
offset: u64,
) -> Result<(), Error>
fn write_all_vectored_at( &self, bufs: &mut [IoSlice<'_>], offset: u64, ) -> Result<(), Error>
write_all_vectored
what write_all_at
is to write_all
.source§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
source§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
self
file descriptor.source§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
source§fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
self
file descriptor. Read moresource§impl<T> IoExt for Twhere
T: AsFilelike AsSocketlike,
impl<T> IoExt for Twhere
T: AsFilelike AsSocketlike,
source§fn read(&self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&self, buf: &mut [u8]) -> Result<usize, Error>
source§fn read_exact(&self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn read_to_end(&self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read moresource§fn read_to_string(&self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&self, buf: &mut String) -> Result<usize, Error>
buf
. Read moresource§fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
source§fn write(&self, buf: &[u8]) -> Result<usize, Error>
fn write(&self, buf: &[u8]) -> Result<usize, Error>
source§fn write_all(&self, buf: &[u8]) -> Result<(), Error>
fn write_all(&self, buf: &[u8]) -> Result<(), Error>
source§fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
write
, except that it writes from a slice of buffers. Read moresource§fn flush(&self) -> Result<(), Error>
fn flush(&self) -> Result<(), Error>
source§fn write_fmt(&self, fmt: Arguments<'_>) -> Result<(), Error>
fn write_fmt(&self, fmt: Arguments<'_>) -> Result<(), Error>
source§fn read_exact_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<(), Error>
fn read_exact_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<(), Error>
read_vectored
what read_exact
is to read
.