[−][src]Struct count_write::CountWrite
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]
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
[src]
fn flush(&mut self) -> Result<(), Error>
[src]
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize, Error>
1.36.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[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
fn poll_write(
self: Pin<&mut Self>,
ctx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_flush(
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
fn poll_close(
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
impl<W: AsyncWrite> AsyncWrite for CountWrite<W>
[src]
Wrapper for tokio_io::AsyncWrite
, used in the tokio
family
Only available with the "tokio"
feature
fn poll_write(
self: Pin<&mut Self>,
ctx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_flush(
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
fn poll_shutdown(
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
ctx: &mut Context
) -> Poll<Result<(), Error>>
fn poll_write_buf<B>(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<Result<usize, Error>> where
B: Buf,
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<Result<usize, Error>> where
B: Buf,
Auto Trait Implementations
impl<W> Send for CountWrite<W> where
W: Send,
W: Send,
impl<W> Sync for CountWrite<W> where
W: Sync,
W: Sync,
impl<W> Unpin for CountWrite<W> where
W: Unpin,
W: Unpin,
impl<W> UnwindSafe for CountWrite<W> where
W: UnwindSafe,
W: UnwindSafe,
impl<W> RefUnwindSafe for CountWrite<W> where
W: RefUnwindSafe,
W: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
W: Write + ?Sized,
fn write_u8(&mut self, n: u8) -> Result<(), Error>
fn write_i8(&mut self, n: i8) -> Result<(), Error>
fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_f32<T>(&mut self, n: f32) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,
fn write_f64<T>(&mut self, n: f64) -> Result<(), Error> where
T: ByteOrder,
T: ByteOrder,