Expand description
poll-promise is a Rust crate for polling the result of a concurrent (e.g. async) operation.
It is particularly useful in games and immediate mode GUI:s, where one often wants to start a background operation and then ask “are we there yet?” on each subsequent frame until the operation completes.
Example:
let promise = Promise::spawn_thread("slow_operation", something_slow);
// Then in the game loop or immediate mode GUI code:
if let Some(result) = promise.ready() {
// Use/show result
} else {
// Show a loading screen
}§Features
poll-promise can be used with any async runtime (or without one!),
but a few convenience methods are added
when compiled with the following features:
-
async-std— If you enable theasync-stdfeature you can usePromise::spawn_asyncandPromise::spawn_blockingwhich will spawn tasks in the surrounding async-std runtime. -
smol— If you enable thesmolfeature you can usePromise::spawn_asyncandPromise::spawn_localwhich will spawn tasks using the smol executor. Remember to tick the smol executor withtickandtick_local. -
smol_tick_poll— Enabling thesmol_tick_pollfeature (together withsmol) callingPromise::pollwill automatically tick the smol executor. This means you do not have to worry about callingtickbut comes at the cost of loss of finer control over the executor.Since calling
tick_localwill block the current thread, running multiple local promises at once withsmol_tick_pollenabled may also cause stuttering.poll-promise will automatically tick the smol executor with this feature disabled for you when using
Promise::block_until_readyand friends, however. -
tokio— If you enable thetokiofeature you can usePromise::spawn_async,Promise::spawn_localandPromise::spawn_blockingwhich will spawn tasks in the surrounding tokio runtime. -
web— If you enable thewebfeature you can usePromise::spawn_localwhich will spawn tasks usingwasm_bindgen_futures::spawn_local.
Structs§
- Promise
- A promise that waits for the reception of a single value, presumably from some async task.
- Sender
- Used to send a result to a
Promise.
Enums§
- Task
Type - The type of a running task.
Functions§
- tick
smol - ‘Tick’ the
smolthread executor. - tick_
local smol - ‘Tick’ the
smollocal thread executor.