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§
- Completion
Stage - Push-based futures for Rust similar to Java’s
CompletionStage. - RefGuard
- Weak
Completion Stage - Weak reference to a completion stage.
Enums§
- Completion
- Enum that represents completion of the stage
- Completion
State - represents the state a
CompletionStagecan be in. - GetTimeout
Result - TryResult
Traits§
- Fallible
Executor - 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
- Infallible
Executor - Static Executor that can only fail to execute something by panicking
- Instanced
Fallible Executor - 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
- Instanced
Infallible Executor - Executor that can only fail to execute something by panicking