Struct io_streams::BufReaderLineWriter [−][src]
pub struct BufReaderLineWriter<Inner: HalfDuplex> { /* fields omitted */ }
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
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(())
}
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(())
}
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(())
}
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(())
}
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
Borrows the handle. Read more
Like AsHandle::as_handle
and AsSocket::as_socket
but can return either type. Read more
Extracts the raw handle, without taking any ownership.
impl<Inner: HalfDuplex + AsRawHandleOrSocket> AsRawHandleOrSocket for BufReaderLineWriter<Inner>
impl<Inner: HalfDuplex + AsRawHandleOrSocket> AsRawHandleOrSocket for BufReaderLineWriter<Inner>
Like AsRawHandle::as_raw_handle
and AsRawSocket::as_raw_socket
but can return either type. Read more
Extracts the underlying raw socket from this object.
Borrows the socket.
Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Tells this buffer that amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read more
Read all bytes into buf
until the delimiter byte
or EOF is reached. Read more
Read all bytes until a newline (the 0xA
byte) is reached, and append
them to the provided buffer. Read more
🔬 This is a nightly-only experimental API. (buf_read_has_data_left
)
recently added
Check if the underlying Read
has any data left to be read. Read more
Returns an iterator over the contents of this reader split on the byte
byte
. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read
, except that it reads into a slice of buffers. Read more
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
read_initializer
)Determines if this Read
er can work with buffers of uninitialized
memory. Read more
Read all bytes until EOF in this source, placing them into buf
. Read more
Read all bytes until EOF in this source, appending them to buf
. Read more
Read the exact number of bytes required to fill buf
. Read more
Creates a “by reference” adapter for this instance of Read
. Read more
Creates an adapter which will chain this stream with another. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
Attempts to write an entire buffer into this writer. Read more
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Auto Trait Implementations
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
Borrows the reference. Read more
pub fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target> where
Target: FromFilelike + IntoFilelike,
pub fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target> where
Target: FromFilelike + IntoFilelike,
Return a borrowing view of a resource which dereferences to a &Target
or &mut Target
. Read more
Extracts the grip.
Extracts the raw grip.
Borrows the reference.
pub fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target> where
Target: FromSocketlike + IntoSocketlike,
pub fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target> where
Target: FromSocketlike + IntoSocketlike,
Return a borrowing view of a resource which dereferences to a &Target
or &mut Target
. Read more
Mutably borrows from an owned value. Read more
Announce the expected access pattern of the data at the given offset.
Allocate space in the file, increasing the file size as needed, and ensuring that there are no holes under the given range. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Read the exact number of bytes required to fill buf
. Read more
Reads a number of bytes starting from a given offset. Read more
Reads the exact number of byte required to fill buf from the given offset. Read more
Like read
, except that it reads into a slice of buffers. Read more
pub fn read_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64
) -> Result<usize, Error>
pub fn read_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64
) -> Result<usize, Error>
Is to read_vectored
what read_at
is to read
.
pub fn read_exact_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64
) -> Result<(), Error>
pub fn read_exact_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64
) -> Result<(), Error>
Is to read_exact_vectored
what read_exact_at
is to read_exact
.
Determines if this Read
er has an efficient read_vectored_at
implementation. Read more
Read all bytes until EOF in this source, placing them into buf
. Read more
Read all bytes, starting at offset
, until EOF in this source, placing
them into buf
. Read more
Read all bytes until EOF in this source, appending them to buf
. Read more
Read all bytes, starting at offset
, until EOF in this source,
appending them to buf
. Read more
Read bytes from the current position without advancing the current position. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Attempts to write an entire buffer into this writer. Read more
Writes a number of bytes starting from a given offset. Read more
Attempts to write an entire buffer starting from a given offset. Read more
Like write
, except that it writes from a slice of buffers. Read more
Is to write_vectored
what write_at
is to write
.
Is to write_all_vectored
what write_all_at
is to write_all
.
Determines if this Write
r has an efficient write_vectored_at
implementation. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Seek to an offset, in bytes, in a stream. Read more
Returns the current seek position from the start of the stream. Read more
Is to read_vectored
what read_exact
is to read
.
Query the “status” flags for the self
file descriptor.
pub fn new_set_fd_flags(
&self,
fd_flags: FdFlags
) -> Result<SetFdFlags<T>, Error> where
T: AsFilelike + FromFilelike,
pub fn new_set_fd_flags(
&self,
fd_flags: FdFlags
) -> Result<SetFdFlags<T>, Error> where
T: AsFilelike + FromFilelike,
Create a new SetFdFlags
value for use with set_fd_flags
. Read more
pub fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error> where
T: AsFilelike,
pub fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error> where
T: AsFilelike,
Set the “status” flags for the self
file descriptor. Read more