Skip to main content

shared_state/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3
4//! Concurrent state utilities for Rust services.
5//!
6//! Provides [`ReadyGate`] for readiness probing, [`ttl::TtlCache`] for time-expiring
7//! caches, [`counter::SharedCounter`] for atomic counters, and type aliases for common
8//! concurrent patterns.
9
10pub mod counter;
11pub mod ready;
12pub mod ttl;
13
14#[cfg(loom)]
15mod loom_tests;
16
17pub use ready::ReadyGate;
18
19/// Type alias for a shared concurrent map backed by `tokio::sync::RwLock`.
20pub type SharedMap<K, V> = std::sync::Arc<tokio::sync::RwLock<std::collections::HashMap<K, V>>>;
21
22/// Type alias for a shared concurrent cache backed by `DashMap`.
23pub type SharedCache<K, V> = std::sync::Arc<dashmap::DashMap<K, V>>;
24
25/// Create a new [`SharedMap`].
26pub fn shared_map<K: Eq + std::hash::Hash, V>() -> SharedMap<K, V> {
27    std::sync::Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()))
28}
29
30/// Create a new [`SharedCache`].
31pub fn shared_cache<K: Eq + std::hash::Hash + Clone, V: Clone>() -> SharedCache<K, V> {
32    std::sync::Arc::new(dashmap::DashMap::new())
33}