progress

Function progress 

Source
pub fn progress<F, Fut>(total: u64, f: F) -> impl Progress<Output = Fut::Output>
where F: FnOnce(ProgressUpdater) -> Fut, Fut: Future,
Available on crate feature std only.
Expand description

Creates a progress-tracked future from a closure.

This function takes a total progress value and a closure that receives a ProgressUpdater. The closure should use the updater to report progress as it executes. The returned future implements Progress and can be used to monitor the progress stream.

ยงExamples

use progressor::{progress, Progress, State};
use futures_util::StreamExt;

let task = progress(100, |mut updater| async move {
    for i in 0..=100 {
        if i == 50 {
            // Pause at halfway point
            updater.pause();
        } else {
            updater.update(i);
        }
        // Do some work...
    }
    "completed"
});

// Monitor progress - no need for Box::pin with Unpin
let mut progress_stream = task.progress();
while let Some(update) = progress_stream.next().await {
    match update.state() {
        State::Working => println!("Progress: {}%", (update.completed_fraction() * 100.0) as u32),
        State::Paused => println!("Task paused at {}%", (update.completed_fraction() * 100.0) as u32),
        State::Completed => println!("Task completed!"),
        State::Cancelled => println!("Task cancelled!"),
    }
}