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§
Sourcetype JoinError: Debug + Display + OptionalSend
type JoinError: Debug + Display + OptionalSend
The error type of Self::JoinHandle.
Sourcetype JoinHandle<T: OptionalSend + 'static>: Future<Output = Result<T, Self::JoinError>> + OptionalSend + OptionalSync + Unpin
type JoinHandle<T: OptionalSend + 'static>: Future<Output = Result<T, Self::JoinError>> + OptionalSend + OptionalSync + Unpin
The return type of Self::spawn.
Sourcetype Sleep: Future<Output = ()> + OptionalSend + OptionalSync
type Sleep: Future<Output = ()> + OptionalSend + OptionalSync
The type that enables the user to sleep in an asynchronous runtime.
Sourcetype TimeoutError: Debug + Display + OptionalSend
type TimeoutError: Debug + Display + OptionalSend
The timeout error type.
Sourcetype Timeout<R, T: Future<Output = R> + OptionalSend>: Future<Output = Result<R, Self::TimeoutError>> + OptionalSend
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.
Sourcetype ThreadLocalRng: Rng
type ThreadLocalRng: Rng
Type of thread-local random number generator.
Sourcetype Mutex<T: OptionalSend + 'static>: Mutex<T>
type Mutex<T: OptionalSend + 'static>: Mutex<T>
The async mutex implementation.
Required Methods§
Sourcefn spawn<T>(future: T) -> Self::JoinHandle<T::Output>
fn spawn<T>(future: T) -> Self::JoinHandle<T::Output>
Spawn a new task.
Sourcefn sleep_until(deadline: Self::Instant) -> Self::Sleep
fn sleep_until(deadline: Self::Instant) -> Self::Sleep
Wait until deadline is reached.
Sourcefn timeout<R, F: Future<Output = R> + OptionalSend>(
duration: Duration,
future: F,
) -> Self::Timeout<R, F>
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.
Sourcefn timeout_at<R, F: Future<Output = R> + OptionalSend>(
deadline: Self::Instant,
future: F,
) -> Self::Timeout<R, F>
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.
Sourcefn is_panic(join_error: &Self::JoinError) -> bool
fn is_panic(join_error: &Self::JoinError) -> bool
Check if the Self::JoinError is panic.
Sourcefn thread_rng() -> Self::ThreadLocalRng
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.
Sourcefn new(threads: usize) -> Self
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.
Sourcefn block_on<F, T>(&mut self, future: F) -> Twhere
F: Future<Output = T>,
T: OptionalSend,
fn block_on<F, T>(&mut self, future: F) -> Twhere
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§
Sourcefn run<F, T>(future: F) -> T
fn run<F, T>(future: F) -> T
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.
Sourcefn spawn_blocking<F, T>(f: F) -> impl Future<Output = Result<T, Error>> + Send
fn spawn_blocking<F, T>(f: F) -> impl Future<Output = Result<T, Error>> + Send
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.