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

progress-streams

为实现Read/Write类型的类型提供进度回调

2个稳定版本

使用旧的Rust 2015

1.1.0 2019年7月25日
1.0.0 2018年10月10日

#1062Rust模式

Download history 371/week @ 2024-03-14 409/week @ 2024-03-21 859/week @ 2024-03-28 359/week @ 2024-04-04 394/week @ 2024-04-11 356/week @ 2024-04-18 369/week @ 2024-04-25 401/week @ 2024-05-02 467/week @ 2024-05-09 404/week @ 2024-05-16 377/week @ 2024-05-23 400/week @ 2024-05-30 375/week @ 2024-06-06 331/week @ 2024-06-13 353/week @ 2024-06-20 225/week @ 2024-06-27

1,349 每月下载量
用于 12 个crate(7个直接使用)

MIT 协议

7KB

progress-streams

一个Rust crate,为实现io::Readio::Write类型的类型提供进度回调。

示例

读取器

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();
    }
}

写入器

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();
    }
}

无运行时依赖