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