#read-write #io-write #progress #callback #io-read #stream #read

progress-streams

Progress callbacks for types which implement Read/Write

2 stable releases

Uses old Rust 2015

1.1.0 Jul 25, 2019
1.0.0 Oct 10, 2018

#824 in Rust patterns

Download history 318/week @ 2024-06-19 313/week @ 2024-06-26 216/week @ 2024-07-03 253/week @ 2024-07-10 296/week @ 2024-07-17 360/week @ 2024-07-24 266/week @ 2024-07-31 299/week @ 2024-08-07 425/week @ 2024-08-14 422/week @ 2024-08-21 442/week @ 2024-08-28 565/week @ 2024-09-04 521/week @ 2024-09-11 504/week @ 2024-09-18 542/week @ 2024-09-25 394/week @ 2024-10-02

2,109 downloads per month
Used in 11 crates (6 directly)

MIT license

7KB

progress-streams

Rust crate to provide progress callbacks for types which implement io::Read or io::Write.

Examples

Reader

extern crate progress_streams;

use progress_streams::ProgressReader;
use std::fs::File;
use std::io::Read;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

fn main() {
    let total = Arc::new(AtomicUsize::new(0));
    let mut file = File::open("/dev/urandom").unwrap();
    let mut reader = ProgressReader::new(&mut file, |progress: usize| {
        total.fetch_add(progress, Ordering::SeqCst);
    });

    {
        let total = total.clone();
        thread::spawn(move || {
            loop {
                println!("Read {} KiB", total.load(Ordering::SeqCst) / 1024);
                thread::sleep(Duration::from_millis(16));
            }
        });
    }

    let mut buffer = [0u8; 8192];
    while total.load(Ordering::SeqCst) < 100 * 1024 * 1024 {
        reader.read(&mut buffer).unwrap();
    }
}

Writer

extern crate progress_streams;

use progress_streams::ProgressWriter;
use std::io::{Cursor, Write};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

fn main() {
    let total = Arc::new(AtomicUsize::new(0));
    let mut file = Cursor::new(Vec::new());
    let mut writer = ProgressWriter::new(&mut file, |progress: usize| {
        total.fetch_add(progress, Ordering::SeqCst);
    });

    {
        let total = total.clone();
        thread::spawn(move || {
            loop {
                println!("Written {} Kib", total.load(Ordering::SeqCst) / 1024);
                thread::sleep(Duration::from_millis(16));
            }
        });
    }

    let buffer = [0u8; 8192];
    while total.load(Ordering::SeqCst) < 1000 * 1024 * 1024 {
        writer.write(&buffer).unwrap();
    }
}

No runtime deps