Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pop-os/progress-streams
Rust crate to extend io::Read & io::Write types with progress callbacks
https://github.com/pop-os/progress-streams
Last synced: 2 months ago
JSON representation
Rust crate to extend io::Read & io::Write types with progress callbacks
- Host: GitHub
- URL: https://github.com/pop-os/progress-streams
- Owner: pop-os
- License: mit
- Created: 2018-10-09T18:40:05.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-25T17:29:07.000Z (over 5 years ago)
- Last Synced: 2024-10-28T01:11:53.845Z (3 months ago)
- Language: Rust
- Size: 4.88 KB
- Stars: 18
- Watchers: 10
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rust-cn - pop-os/progress-streams
- awesome-rust - pop-os/progress-streams
- awesome-rust-zh - pop-os/progress-streams - 给实现`dyn io::Read`要么`dyn io::Write`类型的,进度回调函数。 (库 / 数据结构)
- awesome-rust - pop-os/progress-streams
README
# progress-streams
Rust crate to provide progress callbacks for types which implement `io::Read` or `io::Write`.
## Examples
### Reader
```rust
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
```rust
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();
}
}
```