Rust crate to extend io::Read & io::Write types with progress callbacks

Overview

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();
    }
}
You might also like...
A proof of concept implementation of cyclic data structures in stable, safe, Rust.

A proof of concept implementation of cyclic data structures in stable, safe, Rust. This demonstrates the combined power of the static-rc crate and the

Doubly-Linked List Implementation in Rust

Doubly-Linked List Implementation in Rust Purely for educational and recreational purposes. For real world production please use std::collections::Lin

Rust library for string parsing of basic data structures.

afmt Simple rust library for parsing basic data structures from strings. Usage You can specify string formats to any strucute, via the use of the fmt

Hash Table Implementation in Rust
Hash Table Implementation in Rust

Hash Table Implementation in Rust Purely for educational and recreational purposes. For real world production please use std::collections::HashMap. Fo

SegVec data structure for rust. Similar to Vec, but allocates memory in chunks of increasing size.

segvec This crate provides the SegVec data structure. It is similar to Vec, but allocates memory in chunks of increasing size, referred to as "segment

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Blazing fast immutable collection datatypes for Rust.

imbl Blazing fast immutable collection datatypes for Rust. This is a fork of the im crate, which appears to be unmaintained. (This fork is not current

Algorithms and Data Structures of all kinds written in Rust.

Classic Algorithms in Rust This repo contains the implementation of various classic algorithms for educational purposes in Rust. Right now, it is in i

VLists in rust

Running WORK IN PROGRESS! (0. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh) 1. cargo test Motivation In the urbit memory model every

Owner
Pop!_OS
An Operating System by System76
Pop!_OS
Array helpers for Rust's Vector and String types

array_tool Array helpers for Rust. Some of the most common methods you would use on Arrays made available on Vectors. Polymorphic implementations for

Daniel P. Clark 69 Dec 9, 2022
Generic array types in Rust

generic-array This crate implements generic array types for Rust. Requires minumum Rust version of 1.36.0, or 1.41.0 for From<[T; N]> implementations

Bartłomiej Kamiński 325 Dec 25, 2022
Rust-algorithm-club - Learn algorithms and data structures with Rust

Rust Algorithm Club ?? ?? This repo is under construction. Most materials are written in Chinese. Check it out here if you are able to read Chinese. W

Weihang Lo 360 Dec 28, 2022
Ternary search tree collection in rust

tst Ternary search tree collection in rust with similar API to std::collections as it possible. Ternary search tree is a type of trie (sometimes calle

Alexey Pervushin 20 Dec 7, 2022
A priority queue for Rust with efficient change function.

PriorityQueue This crate implements a Priority Queue with a function to change the priority of an object. Priority and items are stored in an IndexMap

null 139 Dec 30, 2022
K-dimensional tree in Rust for fast geospatial indexing and lookup

kdtree K-dimensional tree in Rust for fast geospatial indexing and nearest neighbors lookup Crate Documentation Usage Benchmark License Usage Add kdtr

Rui Hu 152 Jan 2, 2023
Roaring bitmap implementation for Rust

RoaringBitmap This is not yet production ready. The API should be mostly complete now. This is a Rust port of the Roaring bitmap data structure, initi

Roaring bitmaps: A better compressed bitset 552 Jan 1, 2023
Rust Persistent Data Structures

Rust Persistent Data Structures Rust Persistent Data Structures provides fully persistent data structures with structural sharing. Setup To use rpds a

Diogo Sousa 883 Dec 31, 2022
Parameterized routing for generic resources in Rust

Usher Usher provides an easy way to construct parameterized routing trees in Rust. The nodes of these trees is naturally generic, allowing Usher to le

Isaac Whitfield 34 Oct 22, 2022
RiteLinked - LinkedHashMap & LinkedHashSet in Rust

RiteLinked -- HashMap-like containers that hold their key-value pairs in a user controllable order RiteLinked provides more up to date versions of Lin

Rite Database 52 Aug 19, 2022