Expand description
Enabling multithreading for functions and apps that are purely single threaded.
This was originally a module in my dropbear game engine, however I thought there were barely any libraries that had future queuing. It takes inspiration from Unity and how they handle their events.
§Example
use tokio::runtime::Runtime;
use tokio::time::sleep;
use dropbear_future_queue::FutureQueue;
// requires a tokio thread
let rt = Runtime::new().unwrap();
let _guard = rt.enter();
// create new queue
let queue = FutureQueue::new();
// create a new handle to keep for reference
let handle = queue.push(async move {
sleep(1000).await;
67 + 41
});
// assume this is the event loop
loop {
// executes all the futures in the database
queue.poll();
println!("Current status of compututation: {:?}", queue.get_status(&handle));
// check if it is ready to be taken
if let Some(result) = queue.exchange_as::<i32>(&handle) {
println!("67 + 41 = {}", result);
break;
}
// cleans up any ids not needed anymore.
queue.cleanup()
}Structs§
- Future
Handle - A handle to the future task
- Future
Queue - A queue for polling futures. It is stored in here until
FutureQueue::pollis run.
Enums§
- Future
Status - A status showing the future, used by the [
ResultReceiver] and [ResultSender]
Type Aliases§
- AnyResult
- A clonable generic result. It can/preferred to be downcasted to your specific type.
- BoxFuture
- A type used for a future.
- Future
Storage - A type for storing the queue. It uses a
VecDequeto storeFutureHandle’s andBoxFuture - Throwable
- A type recommended to be used by
FutureQueueto allow being thrown around in your app