#byte #io-write #monitor #numbers

no-std write-monitor

Monitor the number of bytes being written

3 releases

0.0.1-alpha.3 Aug 23, 2023
0.0.1-alpha.2 Aug 22, 2023

#54 in #io-write

MIT license

7KB
112 lines

WriteMonitor

Monitor the amount of bytes written to a tokio::io::AsyncWrite
or a futures::io::AsyncWrite or a std::io::Write


lib.rs:

WriteMonitor will wrap over a writer and monitor how many bytes are written to it. This is useful for showing progress of writes

Example

use write_monitor::WriteMonitor;
use std::io::Write;
let mut buf = std::fs::File::create("somefile").unwrap();
let mut wm = WriteMonitor::new(buf);
let big_data = std::fs::read("Cargo.toml").unwrap();
let big_data_len = big_data.len();
let monitor = wm.monitor();
std::thread::spawn(move || {
    wm.write_all(&big_data).unwrap();
});
let mut last_written = 0;
while monitor.bytes_written() < big_data_len as u64 {
   let written = monitor.bytes_written();
   if written != last_written {
   println!("{} bytes written", written);
   last_written = written;
   }
 std::thread::sleep(std::time::Duration::from_millis(100));
}

Dependencies

~0–5.5MB
~24K SLoC