Struct asynchron::Futurize[][src]

pub struct Futurize<C, T, E> { /* fields omitted */ }
Expand description

Futurize task asynchronously.

Example:

use asynchron::{Futurize, Futurized, ITaskHandle, Progress};
use std::{convert::Infallible, time::{Duration, Instant}};

fn main() {
    let instant: Instant = Instant::now();
    let task: Futurized<String, u32, String> = Futurize::task(
        0,
        move |_task: ITaskHandle<String>| -> Progress<String, u32, String> {
            let sleep_dur = Duration::from_millis(10);
            std::thread::sleep(sleep_dur);
            // Send current task progress.
            let result = Ok::<String, Infallible>("The task  wake up from sleep.".into());
            if let Ok(value) = result {
                _task.send(value);
            } else {
                // return error immediately if something not right, for example:
                return Progress::Error(
                    "Something ain't right..., programmer out of bounds.".into(),
                );
            }
            if _task.is_canceled() {
                let _ = _task.send("Canceling the task".into());
                Progress::Canceled
            } else {
                Progress::Completed(instant.elapsed().subsec_millis())
            }
        },
    );

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

    loop {
        if task.is_in_progress() {
            match task.try_get() {
                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 task.is_done() {
                break;
            }
        }
    }
}

Implementations

Create new task.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.