Skip to main content

Crate completion_stage

Crate completion_stage 

Source
Expand description

§completion_stage

Push-based futures for Rust similar to Java’s CompletionStage.

§Simple Example

use std::thread;
use std::time::Duration;
use completion_stage::CompletionStage;

fn some_func() {
    let result : String = CompletionStage::new_async::<thread::Thread>(|| {
        // Executed in a new virgin thread via thread::spawn,
        // you can provide your own executor via the generic instead of 'thread::Thread'
        // by implementing a simple trait for your executor.
        //
        // Do some background task here
        thread::sleep(Duration::from_secs(5));
        //
        // eventually return the result.
        return 12345;
    }).and_then_apply(|intermediate| {
        // Executed in the same thread as above,
        // or the main thread if the thread above is already finished,
        // which is unlikely for this example
        return format!("The result is {intermediate}");
    }).unwrap();

    println!("{}", result);

    let stage = CompletionStage::new();
    let ref_counted = stage.clone();
    thread::spawn(move || {
        thread::sleep(Duration::from_secs(5));
        ref_counted.complete_with_value(7890123);
    });

    //Blocks until the thread is done...
    let result = stage.unwrap();
    println!("{}", result);
}

Structs§

CompletionStage
Push-based futures for Rust similar to Java’s CompletionStage.
RefGuard
WeakCompletionStage
Weak reference to a completion stage.

Enums§

Completion
Enum that represents completion of the stage
CompletionState
represents the state a CompletionStage can be in.
GetTimeoutResult
TryResult

Traits§

FallibleExecutor
Static Executor that can fail with a given error when trying to execute something. Error refers to an OS error like “I cannot spawn more threads” rather than any error the task may produce
InfallibleExecutor
Static Executor that can only fail to execute something by panicking
InstancedFallibleExecutor
Executor that can fail with a given error when trying to execute something. Error refers to an OS error like “I cannot spawn more threads” rather than any error the task may produce
InstancedInfallibleExecutor
Executor that can only fail to execute something by panicking