completion_stage
Push-based futures for Rust similar to Java's CompletionStage.
Simple Example
use thread;
use Duration;
use CompletionStage;
Motivation
In the rust language 'futures' are poll-based and require use of 'async/await' which hides the callback-based execution flow from the programmer as well as requiring complex execution engines and frameworks that are unsuitable for a lot of situations.
This crate aims to provide futures that require no async execution engine, nor do they attempt to hide callback-based control flow from you.
What does push-based mean?
This example here illustrates what is meant by push-based, functionally it's completely identical to the example above.
use thread;
use Duration;
use CompletionStage;
Panics
This crate does not catch any panic using panic::catch_unwind. If any user code panic's then a "drop guard" ensures that all dependent stages will be completed and in turn have their user code invoked during unwinding with Completion::Panic. This means that if user code panics again during this unwinding, then the application will abort as it normally does on a panic during unwind.
The panic value itself is not caught and therefore not available to dependent stages, as rust panic's should not be used in lieu of exceptions.
If you compile your code with panic=abort then none of this matters to you.
Example of panic
use thread;
use Duration;
use ;
Deadlocks
The implementation automatically detects simple deadlocks. All deadlocks that are caused by a thread waiting on itself are detected. For example, borrowing the value of a stage while trying to take the value from the stage is detected. This generally causes the taking operation to fail. Some taking operation's panic in this case, which is made explicit in their documentation.
Example where Deadlock is detected
use ;
Use-Case
I intend to use this crate to "return" results to an opengl ui-thread. The opengl ui "thread" will start background tasks when, for example, a "button" is pressed but should not be blocked (as that would freeze the UI).
Currently, I have been using a lot of janky code that uses mpsc Channels and try_recv for this purpose, but I intend to replace all such code with this crate.