Struct io_streams::BufReaderLineWriter[][src]

pub struct BufReaderLineWriter<Inner: HalfDuplex> { /* fields omitted */ }

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]

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]

impl<Inner: HalfDuplex> BufRead for BufReaderLineWriter<Inner>[src]

impl<Inner: HalfDuplex> Debug for BufReaderLineWriter<Inner> where
    Inner: Debug
[src]

impl<Inner: HalfDuplex> Read for BufReaderLineWriter<Inner>[src]

impl<Inner: HalfDuplex> Write for BufReaderLineWriter<Inner>[src]

Auto Trait Implementations

impl<Inner> RefUnwindSafe for BufReaderLineWriter<Inner> where
    Inner: RefUnwindSafe
[src]

impl<Inner> Send for BufReaderLineWriter<Inner> where
    Inner: Send
[src]

impl<Inner> Sync for BufReaderLineWriter<Inner> where
    Inner: Sync
[src]

impl<Inner> Unpin for BufReaderLineWriter<Inner> where
    Inner: Unpin
[src]

impl<Inner> UnwindSafe for BufReaderLineWriter<Inner> where
    Inner: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.