Skip to main content

AsyncRuntime

Trait AsyncRuntime 

Source
pub trait AsyncRuntime:
    Debug
    + OptionalSend
    + OptionalSync
    + 'static {
    type JoinError: Debug + Display + OptionalSend;
    type JoinHandle<T: OptionalSend + 'static>: Future<Output = Result<T, Self::JoinError>> + OptionalSend + OptionalSync + Unpin;
    type Sleep: Future<Output = ()> + OptionalSend + OptionalSync;
    type Instant: Instant;
    type TimeoutError: Debug + Display + OptionalSend;
    type Timeout<R, T: Future<Output = R> + OptionalSend>: Future<Output = Result<R, Self::TimeoutError>> + OptionalSend;
    type ThreadLocalRng: Rng;
    type Mpsc: Mpsc;
    type Watch: Watch;
    type Oneshot: Oneshot;
    type Mutex<T: OptionalSend + 'static>: Mutex<T>;

    // Required methods
    fn spawn<T>(future: T) -> Self::JoinHandle<T::Output>
       where T: Future + OptionalSend + 'static,
             T::Output: OptionalSend + 'static;
    fn sleep(duration: Duration) -> Self::Sleep;
    fn sleep_until(deadline: Self::Instant) -> Self::Sleep;
    fn timeout<R, F: Future<Output = R> + OptionalSend>(
        duration: Duration,
        future: F,
    ) -> Self::Timeout<R, F>;
    fn timeout_at<R, F: Future<Output = R> + OptionalSend>(
        deadline: Self::Instant,
        future: F,
    ) -> Self::Timeout<R, F>;
    fn is_panic(join_error: &Self::JoinError) -> bool;
    fn thread_rng() -> Self::ThreadLocalRng;
    fn new(threads: usize) -> Self;
    fn block_on<F, T>(&mut self, future: F) -> T
       where F: Future<Output = T>,
             T: OptionalSend;

    // Provided methods
    fn run<F, T>(future: F) -> T
       where Self: Sized,
             F: Future<Output = T>,
             T: OptionalSend { ... }
    fn spawn_blocking<F, T>(
        f: F,
    ) -> impl Future<Output = Result<T, Error>> + Send
       where F: FnOnce() -> T + Send + 'static,
             T: Send + 'static { ... }
}
Expand description

A trait defining interfaces with an asynchronous runtime.

The intention of this trait is to allow an application using this crate to bind an asynchronous runtime that suits it the best.

Some additional related functions are also exposed by this trait.

§Note

The default asynchronous runtime is tokio.

Required Associated Types§

Source

type JoinError: Debug + Display + OptionalSend

The error type of Self::JoinHandle.

Source

type JoinHandle<T: OptionalSend + 'static>: Future<Output = Result<T, Self::JoinError>> + OptionalSend + OptionalSync + Unpin

The return type of Self::spawn.

Source

type Sleep: Future<Output = ()> + OptionalSend + OptionalSync

The type that enables the user to sleep in an asynchronous runtime.

Source

type Instant: Instant

A measurement of a monotonically non-decreasing clock.

Source

type TimeoutError: Debug + Display + OptionalSend

The timeout error type.

Source

type Timeout<R, T: Future<Output = R> + OptionalSend>: Future<Output = Result<R, Self::TimeoutError>> + OptionalSend

The timeout type used by Self::timeout and Self::timeout_at that enables the user to await the outcome of a Future.

Source

type ThreadLocalRng: Rng

Type of thread-local random number generator.

Source

type Mpsc: Mpsc

The bounded MPSC channel implementation.

Source

type Watch: Watch

The watch channel implementation.

Source

type Oneshot: Oneshot

The oneshot channel implementation.

Source

type Mutex<T: OptionalSend + 'static>: Mutex<T>

The async mutex implementation.

Required Methods§

Source

fn spawn<T>(future: T) -> Self::JoinHandle<T::Output>
where T: Future + OptionalSend + 'static, T::Output: OptionalSend + 'static,

Spawn a new task.

Source

fn sleep(duration: Duration) -> Self::Sleep

Wait until duration has elapsed.

Source

fn sleep_until(deadline: Self::Instant) -> Self::Sleep

Wait until deadline is reached.

Source

fn timeout<R, F: Future<Output = R> + OptionalSend>( duration: Duration, future: F, ) -> Self::Timeout<R, F>

Require a Future to complete before the specified duration has elapsed.

Source

fn timeout_at<R, F: Future<Output = R> + OptionalSend>( deadline: Self::Instant, future: F, ) -> Self::Timeout<R, F>

Require a Future to complete before the specified instant in time.

Source

fn is_panic(join_error: &Self::JoinError) -> bool

Check if the Self::JoinError is panic.

Source

fn thread_rng() -> Self::ThreadLocalRng

Get the random number generator to use for generating random numbers.

§Note

This is a per-thread instance, which cannot be shared across threads or sent to another thread.

Source

fn new(threads: usize) -> Self

Create a new runtime instance for testing purposes.

Note: This method is primarily intended for testing and is not used by Openraft internally. In production applications, the runtime should be created and managed by the application itself, with Openraft running within that runtime.

§Arguments
  • threads - Number of worker threads. Multi-threaded runtimes (like Tokio) will use this value; single-threaded runtimes (like Monoio, Compio) may ignore it.
Source

fn block_on<F, T>(&mut self, future: F) -> T
where F: Future<Output = T>, T: OptionalSend,

Run a future to completion on this runtime.

This runs synchronously on the current thread, so Send is not required.

Provided Methods§

Source

fn run<F, T>(future: F) -> T
where Self: Sized, F: Future<Output = T>, T: OptionalSend,

Convenience method: create a runtime and run the future to completion.

Creates a runtime with default configuration (8 threads) and runs the future. For simple cases where you don’t need to reuse the runtime. If you need to run multiple futures, consider using Self::new and Self::block_on directly.

This runs synchronously on the current thread, so Send is not required.

Source

fn spawn_blocking<F, T>(f: F) -> impl Future<Output = Result<T, Error>> + Send
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Run a blocking function on a separate thread.

The default implementation spawns a new OS thread for each call. Runtime implementations may override this with their own thread pool (e.g., tokio’s spawn_blocking) for better resource management.

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§