Skip to main content

asyncband/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#![cfg_attr(docsrs, feature(doc_cfg))]
19#![deny(missing_docs)]
20
21//! Composable, runtime-agnostic concurrency building blocks for async Rust.
22//!
23//! `asyncband` provides synchronization, initialization, task coordination, channels, resource
24//! reuse, and workload control without choosing an executor for the application. Its async APIs use
25//! standard futures and wakers, so they can run on Tokio, async-std, smol, or a custom executor.
26//!
27//! # Getting started
28//!
29//! Public APIs are enabled through opt-in Cargo features, and no features are enabled by default.
30//! Enable the APIs your application needs:
31//!
32//! ```toml
33//! asyncband = { version = "0.7", features = ["mutex", "oneshot"] }
34//! ```
35//!
36//! Then use the selected APIs directly:
37//!
38//! ```
39//! # #[cfg(feature = "mutex")]
40//! # #[tokio::main]
41//! # async fn main() {
42//! use asyncband::mutex::Mutex;
43//!
44//! let counter = Mutex::new(0);
45//! {
46//!     let mut value = counter.lock().await;
47//!     *value += 1;
48//! }
49//! assert_eq!(*counter.lock().await, 1);
50//! # }
51//! # #[cfg(not(feature = "mutex"))]
52//! # fn main() {}
53//! ```
54//!
55//! # API map
56//!
57//! | Area                 | API                                            | Feature        | Use                                                                                                             |
58//! |----------------------|------------------------------------------------|----------------|-----------------------------------------------------------------------------------------------------------------|
59//! | Locks and conditions | [`Mutex`](mutex::Mutex)                        | `mutex`        | Protect shared data with asynchronous mutual exclusion.                                                         |
60//! |                      | [`RwLock`](rwlock::RwLock)                     | `rwlock`       | Allow multiple readers or one writer.                                                                           |
61//! |                      | [`UpgradableRwLock`](rwlock::UpgradableRwLock) | `rwlock`       | Allow multiple readers or one writer, with one upgradable reader.                                               |
62//! |                      | [`Condvar`](condvar::Condvar)                  | `condvar`      | Wait for notifications while releasing a mutex.                                                                 |
63//! | Coordination         | [`Semaphore`](semaphore::Semaphore)            | `semaphore`    | Limit concurrent work by acquiring permits.                                                                     |
64//! |                      | [`Barrier`](barrier::Barrier)                  | `barrier`      | Synchronize a fixed number of participants at a reusable rendezvous.                                            |
65//! |                      | [`ManualResetEvent`](event::ManualResetEvent)  | `event`        | Signal current and future waits until explicitly reset.                                                         |
66//! |                      | [`AutoResetEvent`](event::AutoResetEvent)      | `event`        | Retain one signal and release one waiter per consumed signal.                                                   |
67//! |                      | [`Latch`](latch::Latch)                        | `latch`        | Wait until a fixed one-way countdown reaches zero.                                                              |
68//! |                      | [`Phaser`](phaser::Phaser)                     | `phaser`       | Coordinate repeated phases with a dynamic participant set.                                                      |
69//! |                      | [`WaitGroup`](waitgroup::WaitGroup)            | `waitgroup`    | Dynamically register participants and wait until all have completed.                                            |
70//! |                      | [`Shutdown`](shutdown::Shutdown)               | `shutdown`     | Request shutdown and wait until all completion guards are dropped.                                              |
71//! | Work coalescing      | [`Once`](once::Once)                           | `once`         | Complete one asynchronous initialization; cancelled or panicked attempts may be retried.                        |
72//! |                      | [`OnceCell`](once::OnceCell)                   | `once-cell`    | Store one value from an access-time initializer; failed, cancelled, or panicked attempts may be retried.        |
73//! |                      | [`LazyCell`](once::LazyCell)                   | `lazy-cell`    | Initialize one value with a stored function and resume the same in-flight future after caller cancellation.     |
74//! |                      | [`OnceMap`](once::OnceMap)                     | `once-map`     | Coalesce work per key and retain each successful value until explicitly removed.                                |
75//! |                      | [`Group`](singleflight::Group)                 | `singleflight` | Coalesce overlapping work per key without retaining completed values.                                           |
76//! | Communication        | [`Completion`](completion::Completion)         | `completion`   | Publish one shared result to any number of current and future observers.                                        |
77//! |                      | [`oneshot`]                                    | `oneshot`      | Send one value from one sender to one receiver.                                                                 |
78//! |                      | [`mpmc`]                                       | `mpmc`         | Distribute each value to exactly one of multiple competing receivers.                                           |
79//! |                      | [`mpsc`]                                       | `mpsc`         | Send each value from multiple producers to one receiver with bounded backpressure or an unbounded queue.        |
80//! |                      | [`spmc`]                                       | `spmc`         | Distribute work from one exclusive sender to multiple competing receivers, with bounded or unbounded storage.   |
81//! |                      | [`broadcast`]                                  | `broadcast`    | Deliver every value to active receivers with bounded backpressure or unbounded retention.                       |
82//! |                      | [`watch`]                                      | `watch`        | Publish cloneable latest state from one or more senders; receivers independently coalesce intermediate updates. |
83//! | Object reuse         | [`pool`]                                       | `pool`         | Reuse objects through bounded or unbounded pool variants.                                                       |
84//! | Sync interop         | [`FutureExt`](blocking::FutureExt)             | `blocking`     | Drive one runtime-agnostic future from a blocking thread.                                                       |
85//!
86//! # Scope and runtime model
87//!
88//! The project is not limited to small or stateless primitives. Stateful tools such as
89//! [`singleflight::Group`] and the [`pool`] module fit when they provide reusable coordination and
90//! remain independent of executor policy.
91//!
92//! The async APIs do not start threads, spawn tasks, install timers, or require a runtime-specific
93//! reactor. Task placement, deadlines, retries, periodic maintenance, and lifecycle orchestration
94//! remain with the caller. Await Asyncband futures inside any executor that polls standard Rust
95//! futures, and compose those runtime services around them.
96//!
97//! Executor waker operations, including cloning, waking, and dropping, are expected not to panic.
98//! Recovery from panicking waker operations is not part of the public API contract.
99//!
100//! # Async first, blocking by adaptation
101//!
102//! Async and synchronous primitives have different optimization constraints. Asyncband designs its
103//! primitives for async use and provides the optional [`blocking`] module as a boundary adapter
104//! instead of duplicating synchronous methods across every type. Sync-first implementations can
105//! exploit OS- or platform-specific facilities and remain the domain of dedicated libraries.
106//!
107//! The adapter polls one future on the calling thread. It is not a general-purpose async runtime,
108//! and futures that depend on a runtime-specific timer or I/O driver may not make progress. See the
109//! module documentation for the full execution constraints.
110//!
111//! # Thread safety
112//!
113//! Asyncband types implement `Send` and `Sync` only when their protected, transferred, or managed
114//! values satisfy the required bounds. Consult each API's documentation for its exact contract.
115//!
116//! # Disclaimer
117//!
118//! Apache Asyncband (Incubating) is an effort undergoing incubation at the Apache Software
119//! Foundation (ASF), sponsored by the Apache Incubator PMC.
120//!
121//! Incubation is required of all newly accepted projects until a further review indicates that the
122//! infrastructure, communications, and decision-making process have stabilized in a manner
123//! consistent with other successful ASF projects.
124//!
125//! While incubation status is not necessarily a reflection of the completeness or stability of the
126//! code, it does indicate that the project has yet to be fully endorsed by the ASF.
127mod internal;
128
129#[cfg(feature = "barrier")]
130pub mod barrier;
131#[cfg(feature = "blocking")]
132pub mod blocking;
133#[cfg(feature = "broadcast")]
134pub mod broadcast;
135#[cfg(feature = "completion")]
136pub mod completion;
137#[cfg(feature = "condvar")]
138pub mod condvar;
139#[cfg(feature = "event")]
140pub mod event;
141#[cfg(feature = "latch")]
142pub mod latch;
143#[cfg(feature = "mpmc")]
144pub mod mpmc;
145#[cfg(feature = "mpsc")]
146pub mod mpsc;
147#[cfg(feature = "mutex")]
148pub mod mutex;
149#[cfg(any(
150    feature = "lazy-cell",
151    feature = "once",
152    feature = "once-cell",
153    feature = "once-map"
154))]
155pub mod once;
156#[cfg(feature = "oneshot")]
157pub mod oneshot;
158#[cfg(feature = "phaser")]
159pub mod phaser;
160#[cfg(feature = "pool")]
161pub mod pool;
162#[cfg(feature = "rwlock")]
163pub mod rwlock;
164#[cfg(feature = "semaphore")]
165pub mod semaphore;
166#[cfg(feature = "shutdown")]
167pub mod shutdown;
168#[cfg(feature = "singleflight")]
169pub mod singleflight;
170#[cfg(feature = "spmc")]
171pub mod spmc;
172#[cfg(feature = "waitgroup")]
173pub mod waitgroup;
174#[cfg(feature = "watch")]
175pub mod watch;
176
177#[cfg(all(
178    test,
179    any(feature = "once-map", feature = "phaser", feature = "singleflight")
180))]
181mod test_support;