pub trait AsyncExec:
Send
+ Sync
+ 'static {
// Required methods
fn spawn<F, R>(&self, f: F) -> impl AsyncJoinHandle<R>
where F: Future<Output = R> + Send + 'static,
R: Send + 'static;
fn spawn_detach<F, R>(&self, f: F)
where F: Future<Output = R> + Send + 'static,
R: Send + 'static;
fn spawn_blocking<F, R>(f: F) -> impl AsyncJoinHandle<R>
where F: FnOnce() -> R + Send + 'static,
R: Send + 'static;
fn block_on<F, R>(&self, f: F) -> R
where F: Future<Output = R> + Send,
R: Send + 'static;
}Expand description
Trait for async runtime execution capabilities.
This trait defines the core execution operations that any async runtime should provide, including spawning tasks, running futures to completion, and detaching tasks.
§Example
use orb::prelude::*;
use std::future::Future;
fn example<R: AsyncExec>(runtime: &R) -> impl Future<Output = ()> {
async move {
// Spawn a task
let handle = runtime.spawn(async {
// Do some async work
42
});
// Wait for the result
let result = handle.join().await.unwrap();
assert_eq!(result, 42);
}
}Required Methods§
Sourcefn spawn<F, R>(&self, f: F) -> impl AsyncJoinHandle<R>
fn spawn<F, R>(&self, f: F) -> impl AsyncJoinHandle<R>
Spawn a task in the background, returning a handle to await its result.
This method creates a new task that runs concurrently with the current task. The returned handle can be used to wait for the task’s completion and retrieve its result.
§Type Parameters
F- The future type to spawnR- The return type of the future
§Parameters
f- The future to spawn
§Returns
A handle that implements AsyncJoinHandle and can be used to await
the task’s result.
Sourcefn spawn_detach<F, R>(&self, f: F)
fn spawn_detach<F, R>(&self, f: F)
Spawn a task and detach it (no handle returned).
This method creates a new task that runs in the background without providing a way to wait for its completion. The task will continue running until it completes or the program exits.
§Type Parameters
F- The future type to spawnR- The return type of the future
§Parameters
f- The future to spawn
Sourcefn spawn_blocking<F, R>(f: F) -> impl AsyncJoinHandle<R>
fn spawn_blocking<F, R>(f: F) -> impl AsyncJoinHandle<R>
Run blocking code in a background thread pool, and return an async join handle
§NOTE:
This method spawn with threal pool provide by runtime in current context, globally. In order for ResolveAddr job which does not have a AsyncExec handle, so this method is static.
§Type Parameters
F- The future type to spawnR- The return type of the future
§Parameters
f- The future to spawn
§Returns
A handle that implements AsyncJoinHandle and can be used to await
the call result.
Sourcefn block_on<F, R>(&self, f: F) -> R
fn block_on<F, R>(&self, f: F) -> R
Run a future to completion on the runtime.
This method blocks the current thread until the provided future completes, returning its result.
§Type Parameters
F- The future type to runR- The return type of the future
§Parameters
f- The future to run to completion
§Returns
The output of the future when it completes.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.