Skip to main content

AsyncExec

Trait AsyncExec 

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

Required Methods§

Source

fn spawn<F, R>(&self, f: F) -> Self::AsyncHandle<R>
where F: Future<Output = R> + Send + 'static, R: Send + 'static,

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 spawn
  • R - 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.

Source

fn spawn_detach<F, R>(&self, f: F)
where F: Future<Output = R> + Send + 'static, R: Send + 'static,

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 spawn
  • R - The return type of the future
§Parameters
  • f - The future to spawn
Source

fn spawn_blocking<F, R>(f: F) -> Self::ThreadHandle<R>
where F: FnOnce() -> R + Send + 'static, R: Send + 'static,

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 spawn
  • R - 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.

Source

fn block_on<F, R>(&self, f: F) -> R
where F: Future<Output = R> + Send, R: Send + 'static,

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 run
  • R - 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.

Implementors§

Source§

impl AsyncExec for TokioRT

Source§

impl<FT, T> AsyncExec for FT
where FT: Deref<Target = T> + Send + Sync + 'static, T: AsyncExec,