pub struct CountingWriter<W> { /* private fields */ }Expand description
Wrapper writer that tracks total bytes written.
This writer wraps any Write implementation and maintains a counter
of the total bytes successfully written. This is useful for:
- Tracking compressed archive size for reports
- Monitoring write progress
- Validating expected output sizes
§Implementation Notes
The counter only increments on successful writes. If a write operation fails partway through, only the successfully written bytes are counted.
§Examples
use exarch_core::io::CountingWriter;
use std::io::Write;
let mut buffer = Vec::new();
let mut writer = CountingWriter::new(&mut buffer);
writer.write_all(b"Hello, ")?;
writer.write_all(b"World!")?;
writer.flush()?;
assert_eq!(writer.total_bytes(), 13);
assert_eq!(buffer, b"Hello, World!");Implementations§
Source§impl<W> CountingWriter<W>
impl<W> CountingWriter<W>
Sourcepub fn total_bytes(&self) -> u64
pub fn total_bytes(&self) -> u64
Returns the total number of bytes successfully written.
This count includes all bytes from successful write operations,
including those from write, write_all, and write_fmt.
§Examples
use exarch_core::io::CountingWriter;
use std::io::Write;
let mut buffer = Vec::new();
let mut writer = CountingWriter::new(&mut buffer);
writer.write_all(b"test")?;
assert_eq!(writer.total_bytes(), 4);
writer.write_all(b"data")?;
assert_eq!(writer.total_bytes(), 8);Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consumes the counting writer and returns the inner writer.
This is useful when you need to retrieve the underlying writer after all writing is complete.
§Examples
use exarch_core::io::CountingWriter;
use std::io::Write;
let buffer = Vec::new();
let mut writer = CountingWriter::new(buffer);
writer.write_all(b"test")?;
let buffer = writer.into_inner();
assert_eq!(buffer, b"test");Sourcepub fn get_ref(&self) -> &W
pub fn get_ref(&self) -> &W
Returns a reference to the inner writer.
§Examples
use exarch_core::io::CountingWriter;
let buffer = Vec::new();
let writer = CountingWriter::new(buffer);
let inner_ref: &Vec<u8> = writer.get_ref();Sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
Returns a mutable reference to the inner writer.
§Safety
If you write to the inner writer directly (bypassing the
CountingWriter), the byte count will not be updated.
§Examples
use exarch_core::io::CountingWriter;
let buffer = Vec::new();
let mut writer = CountingWriter::new(buffer);
let inner_mut: &mut Vec<u8> = writer.get_mut();Trait Implementations§
Source§impl<W: Write> Write for CountingWriter<W>
impl<W: Write> Write for CountingWriter<W>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)