Struct asynchron::Futurize[][src]

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

Futurize task asyncronously.

Implementations

impl<T, E> Futurize<T, E> where
    T: Clone + Send + 'static,
    E: Clone + Send + 'static, 
[src]

pub fn task<F>(closure: F) -> Futurize<T, E> where
    F: Send + Sync + 'static + Fn() -> Futurized<T, E>, 
[src]

Create new Futurized task.

Example:

use asynchron::{Futurize, Futurized};
use std::time::{Duration, Instant};

fn main() {
    let instant1: Instant = Instant::now();
    let mut task1_approx_dur = Duration::from_millis(400);
    let task1 = Futurize::task(move || -> Futurized<Vec<u32>, String> {
        std::thread::sleep(task1_approx_dur);
        let elapsed_content = format!("instant 1 enlapsed by now {}", instant1.elapsed().subsec_millis());
        // for OnError demo, change the line above like so: (will return error)
        // let elapsed_content = format!("notsparatedbyspace{}", instant1.elapsed().subsec_millis();
        let mut vec_ui32 = Vec::new();
        elapsed_content
            .split(" ")
            .for_each(|ch| match ch.trim().parse::<u32>() {
                Ok(ui32) => vec_ui32.push(ui32),
                _ => (),
            });
        if vec_ui32.len() > 0 {
            return Futurized::OnComplete(vec_ui32);
        } else {
            return Futurized::OnError(
                "On task 1 there's an error occured:
                there's no valid u32 in elapsed_content
                at futurize/src/example.rs line 136\n".to_string(),
            );
        }
    });
    task1.try_wake();

    let mut task2_approx_dur = Duration::from_millis(600);
    let instant2: Instant = Instant::now();
    let task2 = Futurize::task(move || -> Futurized<u32, ()> {
        std::thread::sleep(task2_approx_dur);
        return Futurized::OnComplete(instant2.elapsed().subsec_millis());
    });
    task2.try_wake();

    loop {
        if task1.awake() {
            match task1.try_get() {
                Futurized::OnComplete(value) => {
                    task1_approx_dur = instant1.elapsed();
                    println!(
                        "task 1 completed at: {:?} has value: {:?}\n",
                        task1_approx_dur, value
                    );
                }
                Futurized::OnProgress => println!("waiting the task 1 to complete\n"),
                Futurized::OnError(e) => println!("{}", e),
            }
        }

        if task2.awake() {
            match task2.try_get() {
                Futurized::OnComplete(value) => {
                    task2_approx_dur = instant2.elapsed();
                    println!(
                        "task 2 completed at: {:?} has value: {:?}\n",
                        task2_approx_dur, value
                    );
                    break;
                }
                Futurized::OnProgress => println!("waiting the task 2 to complete\n"),
                _ => (),
            }
        }
        std::thread::sleep(Duration::from_millis(100));
    }
    let all_approxed_dur = task2_approx_dur - task1_approx_dur;
    println!(
        "all the tasks are completed {:?} earlier.\n\nOk",
        all_approxed_dur
    )
}

pub fn try_wake(&self)[src]

Try (it won’t block current thread) wake the task and then try get later somewhere.

pub fn get(&self) -> Futurized<T, E>[src]

Wait until the task is completed and then get (careful, this is blocking operation).

pub fn try_get(&self) -> Futurized<T, E>[src]

Try get if the task is awake, it won’t block current thread (non-blocking).

pub fn awake(&self) -> bool[src]

Check if the task is awake.

Trait Implementations

impl<T: Clone, E: Clone> Clone for Futurize<T, E>[src]

Auto Trait Implementations

impl<T, E> !RefUnwindSafe for Futurize<T, E>

impl<T, E> Send for Futurize<T, E> where
    E: Send,
    T: Send

impl<T, E> Sync for Futurize<T, E> where
    E: Send,
    T: Send

impl<T, E> Unpin for Futurize<T, E>

impl<T, E> !UnwindSafe for Futurize<T, E>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.