8 breaking releases

0.9.1 Sep 24, 2021
0.8.2 Aug 16, 2021
0.4.0 Jul 31, 2021

#137 in Asynchronous

Download history 88/week @ 2021-08-15 14/week @ 2021-08-22 1/week @ 2021-08-29 1/week @ 2021-09-05 2/week @ 2021-09-12 47/week @ 2021-09-19 29/week @ 2021-09-26 38/week @ 2021-10-03 26/week @ 2021-10-10 3/week @ 2021-10-17 16/week @ 2021-10-24 25/week @ 2021-11-07 8/week @ 2021-11-14 3/week @ 2021-11-21 35/week @ 2021-11-28

71 downloads per month
Used in egui_extras_lib

MIT/Apache

28KB
438 lines

Asynchron

Crates.io Asynchron documentation CI

Asynchronize blocking operation.

Example

use asynchron::{Futurize, Futurized, ITaskHandle, Progress};
use std::{
    io::Error,
    time::{Duration, Instant},
};

fn main() {
    let instant: Instant = Instant::now();
    let task: Futurized<String, u32> = Futurize::task(
        0,
        move |_task: ITaskHandle<String>| -> Progress<String, u32> {
            // // Panic if need to.
            // // will return Error with a message:
            // // "the task with id: (specific task id) panicked!"
            // std::panic::panic_any("loudness");
            let sleep_dur = Duration::from_millis(10);
            std::thread::sleep(sleep_dur);
            let result = Ok::<String, Error>(
                format!("The task with id: {} wake up from sleep", _task.id()).into(),
            );
            match result {
                Ok(value) => {
                    // Send current task progress.
                    _task.send(value)
                }
                Err(e) => {
                    // Return error immediately if something not right, for example:
                    return Progress::Error(e.to_string().into());
                }
            }

            if _task.should_cancel() {
                _task.send("Canceling the task".into());
                return Progress::Canceled;
            }
            Progress::Completed(instant.elapsed().subsec_millis())
        },
    );

    // Try do the task now.
    task.try_do();

    let mut exit = false;
    loop {
        task.try_resolve(|progress, done| {
            match progress {
                Progress::Current(task_receiver) => {
                    if let Some(value) = task_receiver {
                        println!("{}\n", value)
                    }
                    // // Cancel if need to.
                    // task.cancel()
                }
                Progress::Canceled => {
                    println!("The task was canceled\n")
                }
                Progress::Completed(elapsed) => {
                    println!("The task finished in: {:?} milliseconds\n", elapsed)
                }
                Progress::Error(e) => {
                    println!("{}\n", e)
                }
            }

            if done {
                // This scope act like "finally block", do final things here.
                exit = true
            }
        });

        if exit {
            break;
        }
    }
}

More Examples

Unblock blocking ureq or reqwest with fltk-rs can be found here.

No runtime deps

QXMa