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// `TtlCache` was fully implemented behind `pub mod ttl` but never re-exported
19// from the crate root, so the obvious `use shared_state::TtlCache` failed with
20// an unresolved import pointing nowhere near the real path. Found by the
21// estate-integration `systems_substrate` suite.
22pub use ttl::TtlCache;
23
24/// Type alias for a shared concurrent map backed by `tokio::sync::RwLock`.
25pub type SharedMap<K, V> = std::sync::Arc<tokio::sync::RwLock<std::collections::HashMap<K, V>>>;
26
27/// Type alias for a shared concurrent cache backed by `DashMap`.
28pub type SharedCache<K, V> = std::sync::Arc<dashmap::DashMap<K, V>>;
29
30/// Create a new [`SharedMap`].
31pub fn shared_map<K: Eq + std::hash::Hash, V>() -> SharedMap<K, V> {
32    std::sync::Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()))
33}
34
35/// Create a new [`SharedCache`].
36pub fn shared_cache<K: Eq + std::hash::Hash + Clone, V: Clone>() -> SharedCache<K, V> {
37    std::sync::Arc::new(dashmap::DashMap::new())
38}