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, [`TtlCache`] for time-expiring
7//! caches, [`SharedCounter`] for atomic counters, and type aliases for common
8//! concurrent patterns.
9
10pub mod ready;
11pub mod ttl;
12pub mod counter;
13
14pub use ready::ReadyGate;
15
16/// Type alias for a shared concurrent map backed by `tokio::sync::RwLock`.
17pub type SharedMap<K, V> = std::sync::Arc<tokio::sync::RwLock<std::collections::HashMap<K, V>>>;
18
19/// Type alias for a shared concurrent cache backed by `DashMap`.
20pub type SharedCache<K, V> = std::sync::Arc<dashmap::DashMap<K, V>>;
21
22/// Create a new [`SharedMap`].
23pub fn shared_map<K: Eq + std::hash::Hash, V>() -> SharedMap<K, V> {
24    std::sync::Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()))
25}
26
27/// Create a new [`SharedCache`].
28pub fn shared_cache<K: Eq + std::hash::Hash + Clone, V: Clone>() -> SharedCache<K, V> {
29    std::sync::Arc::new(dashmap::DashMap::new())
30}