genswap/lib.rs
1//! A generation-tracked wrapper around `ArcSwap` that enables consumers to cache
2//! `Arc` references locally and only reload when data has changed.
3//!
4//! # Overview
5//!
6//! This library optimizes read-heavy workloads where shared data is updated
7//! infrequently. Instead of calling `ArcSwap::load_full()` on every access
8//! (which involves atomic refcount operations), consumers check a cheap atomic
9//! generation counter first and only reload when necessary.
10//!
11//! # Example
12//!
13//! ```
14//! use genswap::GenSwap;
15//! use std::sync::Arc;
16//! use std::thread;
17//!
18//! // Shared config that gets updated periodically
19//! let config = Arc::new(GenSwap::new(42u64));
20//!
21//! // Spawn consumer threads
22//! let handles: Vec<_> = (0..4).map(|_| {
23//! let config = Arc::clone(&config);
24//! thread::spawn(move || {
25//! let mut reader = config.reader();
26//!
27//! for _ in 0..1000 {
28//! let value = reader.get();
29//! // Use value - hot path: just an atomic load + comparison
30//! assert!(**value >= 42);
31//! }
32//! })
33//! }).collect();
34//!
35//! for h in handles {
36//! h.join().unwrap();
37//! }
38//! ```
39
40mod generational_swap;
41mod cached_reader;
42
43pub use generational_swap::GenSwap;
44pub use cached_reader::CachedReader;
45
46#[cfg(test)]
47mod tests;