[][src]Struct count_write::CountWrite

pub struct CountWrite<W> { /* fields omitted */ }

A wrapper for std::io::Write (and other optional Write implementations) that counts the total number of bytes written successfully.

use std::io::{BufWriter, Write};

use count_write::CountWrite;

let vec = vec![]; // note that Vec<u8> : Write
let cw = CountWrite::from(vec);
let mut bw = BufWriter::new(cw);
write!(bw, "abc")?;
assert_eq!(bw.into_inner()?.count(), 3);

If the inner writer does not successfully write all bytes, only the successfully written bytes are counted. (This does not affect users who always use write_all, write!, etc.)

use std::io::{Result, Write};

use count_write::CountWrite;;

/// A dummy struct that only accepts half of the input into a Vec.
struct OnlyHalf;

impl Write for OnlyHalf {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
    dbg!(buf);
        Ok((buf.len() + 1) / 2)
    }

    fn flush(&mut self) -> Result<()> { Ok(()) }
}

let mut cw = CountWrite::from(OnlyHalf);
write!(cw, "abc")?; // Here, we keep retrying writing into the writer
assert_eq!(cw.count(), 3);

let mut cw = CountWrite::from(OnlyHalf);
cw.write(b"abc")?; // Here, we only write to the writer once and do not retry
assert_eq!(cw.count(), 2);

Methods

impl<W> CountWrite<W>[src]

pub fn count(&self) -> u64[src]

Returns the number of bytes successfull written so far

pub fn into_inner(self) -> W[src]

Extracts the inner writer, discarding this wrapper

Trait Implementations

impl<W> From<W> for CountWrite<W>[src]

impl<W: Write> Write for CountWrite<W>[src]

impl<W: AsyncWrite + Unpin> AsyncWrite for CountWrite<W>[src]

Wrapper for futures_io::AsyncWrite, used in the futures-preview family

Only available with the "futures" feature

impl<W: AsyncWrite> AsyncWrite for CountWrite<W>[src]

Wrapper for tokio_io::AsyncWrite, used in the tokio family

Only available with the "tokio" feature

Auto Trait Implementations

impl<W> Send for CountWrite<W> where
    W: Send

impl<W> Sync for CountWrite<W> where
    W: Sync

impl<W> Unpin for CountWrite<W> where
    W: Unpin

impl<W> UnwindSafe for CountWrite<W> where
    W: UnwindSafe

impl<W> RefUnwindSafe for CountWrite<W> where
    W: RefUnwindSafe

Blanket Implementations

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

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

impl<T> From<T> for 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.

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

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

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

impl<W> WriteBytesExt for W where
    W: Write + ?Sized