pub struct CountWrite<W> { /* private fields */ }
Expand description
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);
Implementations§
Source§impl<W> CountWrite<W>
impl<W> CountWrite<W>
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Extracts the inner writer, discarding this wrapper
Trait Implementations§
Source§impl<W> From<W> for CountWrite<W>
impl<W> From<W> for CountWrite<W>
Source§impl<W: Write> Write for CountWrite<W>
impl<W: Write> Write for CountWrite<W>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flushes this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl<W> Freeze for CountWrite<W>where
W: Freeze,
impl<W> RefUnwindSafe for CountWrite<W>where
W: RefUnwindSafe,
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,
Blanket Implementations§
Source§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
Mutably borrows from an owned value. Read more