Skip to main content

openraft_rt/
lib.rs

1//! Async runtime abstraction traits for Openraft.
2//!
3//! This crate provides the core traits for async runtime abstraction,
4//! allowing Openraft to work with different async runtimes (tokio, compio, monoio, etc.).
5//!
6//! ## Key Traits
7//!
8//! - [`AsyncRuntime`] - Main runtime abstraction
9//! - [`Instant`] - Time measurement
10//! - [`Mpsc`], [`MpscSender`], [`MpscReceiver`] - Bounded MPSC channels
11//! - [`Mutex`] - Async mutex
12//! - [`Oneshot`], [`OneshotSender`] - One-shot channels
13//! - [`Watch`], [`WatchSender`], [`WatchReceiver`] - Watch channels
14//!
15//! ## Features
16//!
17//! - `single-threaded` - Disables `Send` + `Sync` bounds on [`OptionalSend`] and [`OptionalSync`]
18
19mod async_runtime;
20#[macro_use]
21pub mod task_local;
22pub mod deterministic_rng;
23pub mod instant;
24pub mod mpsc;
25pub mod mutex;
26pub mod oneshot;
27pub mod testing;
28pub mod watch;
29
30pub use async_runtime::AsyncRuntime;
31pub use instant::Instant;
32pub use mpsc::Mpsc;
33pub use mpsc::MpscReceiver;
34pub use mpsc::MpscSender;
35pub use mpsc::MpscWeakSender;
36pub use mpsc::SendError;
37pub use mpsc::TryRecvError;
38pub use mutex::Mutex;
39pub use mutex::OwnedGuard;
40pub use oneshot::Oneshot;
41pub use oneshot::OneshotSender;
42pub use threaded::BoxAny;
43pub use threaded::BoxAsyncOnceMut;
44pub use threaded::BoxFuture;
45pub use threaded::BoxIterator;
46pub use threaded::BoxMaybeAsyncOnceMut;
47pub use threaded::BoxOnce;
48pub use threaded::BoxStream;
49pub use threaded::OptionalSend;
50pub use threaded::OptionalSync;
51pub use watch::RecvError;
52pub use watch::Watch;
53pub use watch::WatchReceiver;
54pub use watch::WatchSender;
55
56#[cfg(not(feature = "single-threaded"))]
57mod threaded {
58    use std::any::Any;
59    use std::future::Future;
60    use std::pin::Pin;
61
62    use futures_util::Stream;
63
64    /// A trait that is empty if the `single-threaded` feature flag is enabled,
65    /// otherwise it extends `Send`.
66    pub trait OptionalSend: Send {}
67    impl<T: Send + ?Sized> OptionalSend for T {}
68
69    /// A trait that is empty if the `single-threaded` feature flag is enabled,
70    /// otherwise it extends `Sync`.
71    pub trait OptionalSync: Sync {}
72    impl<T: Sync + ?Sized> OptionalSync for T {}
73
74    /// Type alias for a boxed iterator that is `Send`.
75    pub type BoxIterator<'a, T> = Box<dyn Iterator<Item = T> + Send + 'a>;
76    /// Type alias for a boxed pinned future that is `Send`.
77    pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
78    /// Type alias for a boxed pinned stream that is `Send`.
79    pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
80    /// Type alias for a boxed async function that mutates its argument and is `Send`.
81    pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + Send + 'a>;
82    /// Type alias for a boxed function that optionally returns an async future.
83    pub type BoxMaybeAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> Option<BoxFuture<T>> + Send + 'a>;
84    /// Type alias for a boxed function that takes an argument and is `Send`.
85    pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + Send + 'a>;
86    /// Type alias for a boxed value that is `Send` and can be any type.
87    pub type BoxAny = Box<dyn Any + Send>;
88}
89
90#[cfg(feature = "single-threaded")]
91mod threaded {
92    use std::any::Any;
93    use std::future::Future;
94    use std::pin::Pin;
95
96    use futures_util::Stream;
97
98    /// A trait that is empty if the `single-threaded` feature flag is enabled,
99    /// otherwise it extends `Send`.
100    pub trait OptionalSend {}
101    impl<T: ?Sized> OptionalSend for T {}
102
103    /// A trait that is empty if the `single-threaded` feature flag is enabled,
104    /// otherwise it extends `Sync`.
105    pub trait OptionalSync {}
106    impl<T: ?Sized> OptionalSync for T {}
107
108    /// Type alias for a boxed iterator.
109    pub type BoxIterator<'a, T> = Box<dyn Iterator<Item = T> + 'a>;
110    /// Type alias for a boxed pinned future.
111    pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + 'a>>;
112    /// Type alias for a boxed pinned stream.
113    pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;
114    /// Type alias for a boxed async function that mutates its argument.
115    pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + 'a>;
116    /// Type alias for a boxed function that optionally returns an async future.
117    pub type BoxMaybeAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> Option<BoxFuture<T>> + 'a>;
118    /// Type alias for a boxed function that takes an argument.
119    pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + 'a>;
120    /// Type alias for a boxed value that can be any type.
121    pub type BoxAny = Box<dyn Any>;
122}