ranked_semaphore/lib.rs
1//! # ranked-semaphore
2//!
3//! **A priority-aware semaphore for async Rust.**
4//!
5//! ## Features
6//! - Priority scheduling: Configurable priority-based task ordering
7//! - No runtime dependency: Works with any async runtime (Tokio, async-std, smol, etc.)
8//! - Flexible queue strategies: FIFO/LIFO and custom strategy
9//! - High performance: Optimized for low latency and high throughput
10//!
11//! ## Quick Start
12//! ```rust
13//! use ranked_semaphore::RankedSemaphore;
14//!
15//! #[tokio::main]
16//! async fn main() {
17//! // Create a semaphore with 3 permits, FIFO strategy
18//! let sem = RankedSemaphore::new_fifo(3);
19//!
20//! // Acquire a permit (default priority)
21//! let permit = sem.acquire().await.unwrap();
22//!
23//! // Acquire with custom priority
24//! let high = sem.acquire_with_priority(10).await.unwrap();
25//!
26//! // Release permits automatically on drop
27//! drop(permit);
28//! drop(high);
29//! }
30//! ```
31//!
32//! ## Advanced Usage
33//!
34//! Use [`PriorityConfig`] and [`QueueStrategy`] for fine-grained control:
35//!
36//! ```rust
37//! use ranked_semaphore::{RankedSemaphore, PriorityConfig, QueueStrategy};
38//! use std::sync::Arc;
39//!
40//! #[tokio::main]
41//! async fn main() {
42//! let config = PriorityConfig::new()
43//! .default_strategy(QueueStrategy::Fifo)
44//! .exact(10, QueueStrategy::Lifo);
45//!
46//! let limiter = Arc::new(RankedSemaphore::new_with_config(2, config));
47//!
48//! let _admin = limiter.acquire_with_priority(10).await.unwrap();
49//! let _guest = limiter.acquire_with_priority(0).await.unwrap();
50//! }
51//! ```
52//!
53//! See the [README](https://github.com/yeungkc/ranked-semaphore#readme) and [API docs](https://docs.rs/ranked-semaphore) for more details.
54#![cfg_attr(docsrs, feature(doc_cfg))]
55#![warn(missing_docs, unreachable_pub, missing_debug_implementations)]
56#![deny(rust_2018_idioms)]
57
58mod config;
59mod error;
60mod semaphore;
61mod wait_queue {
62 pub(crate) mod queue;
63 pub(crate) mod waiter;
64 pub(crate) mod waker;
65}
66
67pub use config::{PriorityConfig, QueueStrategy};
68pub use error::{AcquireError, TryAcquireError};
69pub use semaphore::{OwnedRankedSemaphorePermit, RankedSemaphore, RankedSemaphorePermit};