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;
20pub mod instant;
21pub mod mpsc;
22pub mod mutex;
23pub mod oneshot;
24pub mod testing;
25pub mod watch;
26
27pub use async_runtime::AsyncRuntime;
28pub use instant::Instant;
29pub use mpsc::Mpsc;
30pub use mpsc::MpscReceiver;
31pub use mpsc::MpscSender;
32pub use mpsc::MpscWeakSender;
33pub use mpsc::SendError;
34pub use mpsc::TryRecvError;
35pub use mutex::Mutex;
36pub use mutex::OwnedGuard;
37pub use oneshot::Oneshot;
38pub use oneshot::OneshotSender;
39pub use threaded::BoxAny;
40pub use threaded::BoxAsyncOnceMut;
41pub use threaded::BoxFuture;
42pub use threaded::BoxIterator;
43pub use threaded::BoxMaybeAsyncOnceMut;
44pub use threaded::BoxOnce;
45pub use threaded::BoxStream;
46pub use threaded::OptionalSend;
47pub use threaded::OptionalSync;
48pub use watch::RecvError;
49pub use watch::Watch;
50pub use watch::WatchReceiver;
51pub use watch::WatchSender;
52
53#[cfg(not(feature = "single-threaded"))]
54mod threaded {
55    use std::any::Any;
56    use std::future::Future;
57    use std::pin::Pin;
58
59    use futures_util::Stream;
60
61    /// A trait that is empty if the `single-threaded` feature flag is enabled,
62    /// otherwise it extends `Send`.
63    pub trait OptionalSend: Send {}
64    impl<T: Send + ?Sized> OptionalSend for T {}
65
66    /// A trait that is empty if the `single-threaded` feature flag is enabled,
67    /// otherwise it extends `Sync`.
68    pub trait OptionalSync: Sync {}
69    impl<T: Sync + ?Sized> OptionalSync for T {}
70
71    /// Type alias for a boxed iterator that is `Send`.
72    pub type BoxIterator<'a, T> = Box<dyn Iterator<Item = T> + Send + 'a>;
73    /// Type alias for a boxed pinned future that is `Send`.
74    pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
75    /// Type alias for a boxed pinned stream that is `Send`.
76    pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
77    /// Type alias for a boxed async function that mutates its argument and is `Send`.
78    pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + Send + 'a>;
79    /// Type alias for a boxed function that optionally returns an async future.
80    pub type BoxMaybeAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> Option<BoxFuture<T>> + Send + 'a>;
81    /// Type alias for a boxed function that takes an argument and is `Send`.
82    pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + Send + 'a>;
83    /// Type alias for a boxed value that is `Send` and can be any type.
84    pub type BoxAny = Box<dyn Any + Send>;
85}
86
87#[cfg(feature = "single-threaded")]
88mod threaded {
89    use std::any::Any;
90    use std::future::Future;
91    use std::pin::Pin;
92
93    use futures_util::Stream;
94
95    /// A trait that is empty if the `single-threaded` feature flag is enabled,
96    /// otherwise it extends `Send`.
97    pub trait OptionalSend {}
98    impl<T: ?Sized> OptionalSend for T {}
99
100    /// A trait that is empty if the `single-threaded` feature flag is enabled,
101    /// otherwise it extends `Sync`.
102    pub trait OptionalSync {}
103    impl<T: ?Sized> OptionalSync for T {}
104
105    /// Type alias for a boxed iterator.
106    pub type BoxIterator<'a, T> = Box<dyn Iterator<Item = T> + 'a>;
107    /// Type alias for a boxed pinned future.
108    pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + 'a>>;
109    /// Type alias for a boxed pinned stream.
110    pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;
111    /// Type alias for a boxed async function that mutates its argument.
112    pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + 'a>;
113    /// Type alias for a boxed function that optionally returns an async future.
114    pub type BoxMaybeAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> Option<BoxFuture<T>> + 'a>;
115    /// Type alias for a boxed function that takes an argument.
116    pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + 'a>;
117    /// Type alias for a boxed value that can be any type.
118    pub type BoxAny = Box<dyn Any>;
119}