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: AsyncWrite + Unpin> AsyncWrite for CountWrite<W>
Wrapper for futures_io::AsyncWrite
, used in the futures-preview
family
impl<W: AsyncWrite + Unpin> AsyncWrite for CountWrite<W>
Wrapper for futures_io::AsyncWrite
, used in the futures-preview
family
Only available with the "futures"
feature
Source§fn poll_write(
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, Error>>
fn poll_write( self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, Error>>
Attempt to write bytes from
buf
into the object. Read moreSource§fn poll_flush(
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
) -> Poll<Result<(), Error>>
fn poll_flush( self: Pin<&mut Self>, ctx: &mut Context<'_>, ) -> Poll<Result<(), Error>>
Attempt to flush the object, ensuring that any buffered data reach
their destination. Read more
Source§impl<W: AsyncWrite> AsyncWrite for CountWrite<W>
Wrapper for tokio_io::AsyncWrite
, used in the tokio
family
impl<W: AsyncWrite> AsyncWrite for CountWrite<W>
Wrapper for tokio_io::AsyncWrite
, used in the tokio
family
Only available with the "tokio"
feature
Source§fn poll_write(
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, Error>>
fn poll_write( self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, Error>>
Attempt to write bytes from
buf
into the object. Read moreSource§fn poll_flush(
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
) -> Poll<Result<(), Error>>
fn poll_flush( self: Pin<&mut Self>, ctx: &mut Context<'_>, ) -> Poll<Result<(), Error>>
Attempt to flush the object, ensuring that any buffered data reach
their destination. Read more
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
Source§impl<W> WriteBytesExt for W
impl<W> WriteBytesExt for W
Source§fn write_u8(&mut self, n: u8) -> Result<(), Error>
fn write_u8(&mut self, n: u8) -> Result<(), Error>
Writes an unsigned 8 bit integer to the underlying writer. Read more
Source§fn write_i8(&mut self, n: i8) -> Result<(), Error>
fn write_i8(&mut self, n: i8) -> Result<(), Error>
Writes a signed 8 bit integer to the underlying writer. Read more
Source§fn write_u16<T>(&mut self, n: u16) -> Result<(), Error>where
T: ByteOrder,
fn write_u16<T>(&mut self, n: u16) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 16 bit integer to the underlying writer. Read more
Source§fn write_i16<T>(&mut self, n: i16) -> Result<(), Error>where
T: ByteOrder,
fn write_i16<T>(&mut self, n: i16) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 16 bit integer to the underlying writer. Read more
Source§fn write_u24<T>(&mut self, n: u32) -> Result<(), Error>where
T: ByteOrder,
fn write_u24<T>(&mut self, n: u32) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 24 bit integer to the underlying writer. Read more
Source§fn write_i24<T>(&mut self, n: i32) -> Result<(), Error>where
T: ByteOrder,
fn write_i24<T>(&mut self, n: i32) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 24 bit integer to the underlying writer. Read more
Source§fn write_u32<T>(&mut self, n: u32) -> Result<(), Error>where
T: ByteOrder,
fn write_u32<T>(&mut self, n: u32) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 32 bit integer to the underlying writer. Read more
Source§fn write_i32<T>(&mut self, n: i32) -> Result<(), Error>where
T: ByteOrder,
fn write_i32<T>(&mut self, n: i32) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 32 bit integer to the underlying writer. Read more
Source§fn write_u48<T>(&mut self, n: u64) -> Result<(), Error>where
T: ByteOrder,
fn write_u48<T>(&mut self, n: u64) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 48 bit integer to the underlying writer. Read more
Source§fn write_i48<T>(&mut self, n: i64) -> Result<(), Error>where
T: ByteOrder,
fn write_i48<T>(&mut self, n: i64) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 48 bit integer to the underlying writer. Read more
Source§fn write_u64<T>(&mut self, n: u64) -> Result<(), Error>where
T: ByteOrder,
fn write_u64<T>(&mut self, n: u64) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 64 bit integer to the underlying writer. Read more
Source§fn write_i64<T>(&mut self, n: i64) -> Result<(), Error>where
T: ByteOrder,
fn write_i64<T>(&mut self, n: i64) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 64 bit integer to the underlying writer. Read more
Source§fn write_u128<T>(&mut self, n: u128) -> Result<(), Error>where
T: ByteOrder,
fn write_u128<T>(&mut self, n: u128) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned 128 bit integer to the underlying writer.
Source§fn write_i128<T>(&mut self, n: i128) -> Result<(), Error>where
T: ByteOrder,
fn write_i128<T>(&mut self, n: i128) -> Result<(), Error>where
T: ByteOrder,
Writes a signed 128 bit integer to the underlying writer.
Source§fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
Source§fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more
Source§fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
Source§fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error>where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more