Crate dropbear_future_queue

Crate dropbear_future_queue 

Source
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§

FutureHandle
A handle to the future task
FutureQueue
A queue for polling futures. It is stored in here until FutureQueue::poll is run.

Enums§

FutureStatus
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.
FutureStorage
A type for storing the queue. It uses a VecDeque to store FutureHandle’s and BoxFuture
Throwable
A type recommended to be used by FutureQueue to allow being thrown around in your app