pub trait AsyncExec:
Send
+ Sync
+ 'static {
type AsyncHandle<R: Send>: AsyncHandle<R>;
type ThreadHandle<R: Send>: ThreadHandle<R> + Send;
// Required methods
fn spawn<F, R>(&self, f: F) -> Self::AsyncHandle<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) -> Self::ThreadHandle<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.await.unwrap();
assert_eq!(result, 42);
}
}Required Associated Types§
type AsyncHandle<R: Send>: AsyncHandle<R>
type ThreadHandle<R: Send>: ThreadHandle<R> + Send
Required Methods§
Sourcefn spawn<F, R>(&self, f: F) -> Self::AsyncHandle<R>
fn spawn<F, R>(&self, f: F) -> Self::AsyncHandle<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.
§NOTE:
The return AsyncHandle adopts the behavior of tokio.
The behavior of panic varies for runtimes:
- tokio will capture handle to task result,
- async-executor (smol) will not capture panic, the program will exit
§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 AsyncHandle 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.
§NOTE:
The behavior of panic varies for runtimes:
- tokio will ignore other tasks panic after detached,
- async-executor (smol) will not capture panic by default, the program will exit. There’s a feature switch in orb-smol to change this behavior.
§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) -> Self::ThreadHandle<R>
fn spawn_blocking<F, R>(f: F) -> Self::ThreadHandle<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 ThreadHandle 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.