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 file descriptor. Read more

Extracts the raw file descriptor. Read more

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

Returns an iterator over the lines of this reader. Read more

Formats the value using the given formatter. 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

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader 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” adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

Creates an adaptor which will chain this stream with another. Read more

Creates an adaptor which will read at most limit bytes from it. 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

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (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

Creates a “by reference” adaptor for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Borrows the reference. Read more

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.

Return a borrowing view of a resource which dereferences to a &Target or &mut Target. Read more

Immutably borrows from an owned value. 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

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

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.

Is to read_vectored what read_at is to read.

Is to read_exact_vectored what read_exact_at is to read_exact.

Determines if this Reader has an efficient read_vectored_at implementation.

Is to write_vectored what write_all is to write.

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 Writer has an efficient write_vectored_at implementation.

Performs the conversion.

Query the “status” flags for the self file descriptor.

Create a new SetFdFlags value for use with set_fd_flags. Read more

Set the “status” flags for the self file descriptor. Read more

Performs the conversion.

Test whether the handle is readable and/or writable.

Test whether this output stream is attached to a terminal. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.