armature-core 0.8.2

High-performance async HTTP framework core - routing, handlers, middleware
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
//! Copy-on-Write State Management
//!
//! This module provides efficient state management patterns using Arc<T>
//! that avoid cloning data on read operations. Writes create new versions,
//! while reads simply clone the Arc pointer.
//!
//! # Key Types
//!
//! - [`CowState<T>`]: Copy-on-Write state with cheap reads
//! - [`Snapshot<T>`]: Immutable snapshot from a point in time
//! - [`VersionedState<T>`]: State with version tracking for cache invalidation
//! - [`AtomicState<T>`]: State with cheap snapshot reads and swap updates
//!
//! # Performance
//!
//! - **Read**: O(1) - Just Arc clone (atomic increment)
//! - **Write**: O(n) - Clone data + atomic swap
//! - **Memory**: One allocation per version
//!
//! # Example
//!
//! ```rust,ignore
//! use armature_core::cow_state::{CowState, Snapshot};
//!
//! #[derive(Clone)]
//! struct Config {
//!     max_connections: usize,
//!     timeout_ms: u64,
//! }
//!
//! // Create mutable state
//! let state = CowState::new(Config {
//!     max_connections: 100,
//!     timeout_ms: 5000,
//! });
//!
//! // Cheap read (just Arc clone)
//! let snapshot: Snapshot<Config> = state.snapshot();
//! println!("Max: {}", snapshot.max_connections);
//!
//! // Update creates new version
//! state.update(|config| {
//!     config.max_connections = 200;
//! });
//!
//! // Old snapshot still valid
//! assert_eq!(snapshot.max_connections, 100);
//!
//! // New snapshot sees update
//! let new_snapshot = state.snapshot();
//! assert_eq!(new_snapshot.max_connections, 200);
//! ```

use std::ops::Deref;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};

// ============================================================================
// Snapshot - Immutable View
// ============================================================================

/// An immutable snapshot of state at a point in time.
///
/// Snapshots are cheap to clone (just Arc increment) and provide
/// read-only access to the state. Multiple snapshots can exist
/// simultaneously, each seeing the state as it was when created.
///
/// # Example
///
/// ```rust,ignore
/// let state = CowState::new(vec![1, 2, 3]);
/// let snap1 = state.snapshot();
///
/// state.update(|v| v.push(4));
///
/// let snap2 = state.snapshot();
///
/// // snap1 still sees old state
/// assert_eq!(snap1.len(), 3);
/// // snap2 sees new state
/// assert_eq!(snap2.len(), 4);
/// ```
#[derive(Debug)]
pub struct Snapshot<T> {
    /// The actual data, wrapped in Arc for cheap cloning
    inner: Arc<T>,
    /// Version number when snapshot was taken
    version: u64,
}

impl<T> Snapshot<T> {
    /// Create a new snapshot from data.
    #[inline]
    pub fn new(data: T) -> Self {
        Self {
            inner: Arc::new(data),
            version: 0,
        }
    }

    /// Create from Arc with version.
    #[inline]
    pub(crate) fn from_arc(arc: Arc<T>, version: u64) -> Self {
        Self {
            inner: arc,
            version,
        }
    }

    /// Get the version number of this snapshot.
    #[inline]
    pub fn version(&self) -> u64 {
        self.version
    }

    /// Get the inner Arc.
    #[inline]
    pub fn into_arc(self) -> Arc<T> {
        self.inner
    }

    /// Get reference to inner Arc.
    #[inline]
    pub fn as_arc(&self) -> &Arc<T> {
        &self.inner
    }

    /// Check if this snapshot is from the same source as another.
    #[inline]
    pub fn same_source(&self, other: &Snapshot<T>) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }

    /// Get strong reference count.
    #[inline]
    pub fn ref_count(&self) -> usize {
        Arc::strong_count(&self.inner)
    }
}

impl<T> Clone for Snapshot<T> {
    #[inline]
    fn clone(&self) -> Self {
        COW_STATS.record_snapshot_clone();
        Self {
            inner: Arc::clone(&self.inner),
            version: self.version,
        }
    }
}

impl<T> Deref for Snapshot<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> AsRef<T> for Snapshot<T> {
    #[inline]
    fn as_ref(&self) -> &T {
        &self.inner
    }
}

// ============================================================================
// CowState - Copy-on-Write State
// ============================================================================

/// Copy-on-Write state container.
///
/// Provides efficient read access through snapshots (Arc clones) while
/// writes create new versions. This is ideal for configuration, caches,
/// and other read-heavy state.
///
/// # Thread Safety
///
/// - Reads (snapshot): Lock-free, concurrent
/// - Writes (update): Serialized through RwLock
///
/// # Example
///
/// ```rust,ignore
/// use armature_core::cow_state::CowState;
///
/// let state = CowState::new(vec!["a", "b", "c"]);
///
/// // Multiple threads can read concurrently
/// let handles: Vec<_> = (0..10).map(|_| {
///     let snap = state.snapshot();
///     std::thread::spawn(move || {
///         println!("{:?}", *snap);
///     })
/// }).collect();
///
/// // Update (serialized)
/// state.update(|v| v.push("d"));
/// ```
pub struct CowState<T> {
    /// Current state wrapped in Arc for cheap cloning
    current: RwLock<Arc<T>>,
    /// Version counter
    version: AtomicU64,
}

impl<T> CowState<T> {
    /// Create new CoW state.
    pub fn new(value: T) -> Self {
        COW_STATS.record_state_created();
        Self {
            current: RwLock::new(Arc::new(value)),
            version: AtomicU64::new(1),
        }
    }

    /// Get a snapshot of the current state.
    ///
    /// This is very cheap - just an Arc clone.
    #[inline]
    pub fn snapshot(&self) -> Snapshot<T> {
        let arc = self.current.read().unwrap();
        let version = self.version.load(Ordering::Acquire);
        COW_STATS.record_snapshot_taken();
        Snapshot::from_arc(Arc::clone(&arc), version)
    }

    /// Get current version number.
    #[inline]
    pub fn version(&self) -> u64 {
        self.version.load(Ordering::Acquire)
    }

    /// Check if version matches.
    #[inline]
    pub fn is_version(&self, version: u64) -> bool {
        self.version() == version
    }
}

impl<T: Clone> CowState<T> {
    /// Update state by applying a function.
    ///
    /// This clones the current state, applies the function, then
    /// atomically swaps to the new version.
    pub fn update<F>(&self, f: F)
    where
        F: FnOnce(&mut T),
    {
        let mut guard = self.current.write().unwrap();
        let mut new_value = (**guard).clone();
        f(&mut new_value);
        *guard = Arc::new(new_value);
        self.version.fetch_add(1, Ordering::Release);
        COW_STATS.record_update();
    }

    /// Replace state entirely.
    pub fn replace(&self, value: T) {
        let mut guard = self.current.write().unwrap();
        *guard = Arc::new(value);
        self.version.fetch_add(1, Ordering::Release);
        COW_STATS.record_update();
    }

    /// Update state only if predicate is true.
    ///
    /// Returns true if update was applied.
    pub fn update_if<P, F>(&self, predicate: P, f: F) -> bool
    where
        P: FnOnce(&T) -> bool,
        F: FnOnce(&mut T),
    {
        let mut guard = self.current.write().unwrap();
        if predicate(&guard) {
            let mut new_value = (**guard).clone();
            f(&mut new_value);
            *guard = Arc::new(new_value);
            self.version.fetch_add(1, Ordering::Release);
            COW_STATS.record_update();
            true
        } else {
            false
        }
    }

    /// Compare-and-swap: update only if version matches.
    ///
    /// Returns Ok(new_version) if successful, Err(current_version) if version mismatch.
    pub fn compare_and_swap<F>(&self, expected_version: u64, f: F) -> Result<u64, u64>
    where
        F: FnOnce(&mut T),
    {
        let mut guard = self.current.write().unwrap();
        let current = self.version.load(Ordering::Acquire);
        if current != expected_version {
            return Err(current);
        }
        let mut new_value = (**guard).clone();
        f(&mut new_value);
        *guard = Arc::new(new_value);
        let new_version = self.version.fetch_add(1, Ordering::Release) + 1;
        COW_STATS.record_update();
        Ok(new_version)
    }
}

impl<T: Clone + Default> Default for CowState<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for CowState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let snap = self.current.read().unwrap();
        f.debug_struct("CowState")
            .field("value", &**snap)
            .field("version", &self.version.load(Ordering::Relaxed))
            .finish()
    }
}

// ============================================================================
// VersionedState - State with Cache Invalidation
// ============================================================================

/// State with version tracking for cache invalidation.
///
/// Useful when you need to know if state has changed since
/// you last read it (e.g., for caching derived data).
///
/// # Example
///
/// ```rust,ignore
/// use armature_core::cow_state::VersionedState;
///
/// let state = VersionedState::new(Config::default());
///
/// // Cache with version
/// let cached = state.read();
/// let cache_version = state.version();
///
/// // Later, check if cache is stale
/// if state.version() != cache_version {
///     // Re-read and update cache
/// }
/// ```
pub struct VersionedState<T> {
    /// The state value
    inner: CowState<T>,
    /// Name for debugging
    #[allow(dead_code)]
    name: &'static str,
}

impl<T> VersionedState<T> {
    /// Create new versioned state.
    pub fn new(value: T) -> Self {
        Self {
            inner: CowState::new(value),
            name: std::any::type_name::<T>(),
        }
    }

    /// Create with custom name.
    pub fn with_name(value: T, name: &'static str) -> Self {
        Self {
            inner: CowState::new(value),
            name,
        }
    }

    /// Get current version.
    #[inline]
    pub fn version(&self) -> u64 {
        self.inner.version()
    }

    /// Get a snapshot.
    #[inline]
    pub fn read(&self) -> Snapshot<T> {
        self.inner.snapshot()
    }

    /// Get snapshot with version.
    #[inline]
    pub fn read_versioned(&self) -> (Snapshot<T>, u64) {
        let snap = self.inner.snapshot();
        let version = snap.version();
        (snap, version)
    }

    /// Check if state has changed since version.
    #[inline]
    pub fn changed_since(&self, version: u64) -> bool {
        self.version() != version
    }
}

impl<T: Clone> VersionedState<T> {
    /// Update state.
    pub fn write<F>(&self, f: F)
    where
        F: FnOnce(&mut T),
    {
        self.inner.update(f);
    }

    /// Replace state entirely.
    pub fn set(&self, value: T) {
        self.inner.replace(value);
    }

    /// Conditional update based on version.
    pub fn write_if_version<F>(&self, version: u64, f: F) -> Result<u64, u64>
    where
        F: FnOnce(&mut T),
    {
        self.inner.compare_and_swap(version, f)
    }
}

impl<T: Clone + Default> Default for VersionedState<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for VersionedState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VersionedState")
            .field("inner", &self.inner)
            .field("name", &self.name)
            .finish()
    }
}

// ============================================================================
// AtomicState - Snapshot Swap Updates
// ============================================================================

/// State holder with cheap snapshot reads and whole-value swap updates.
///
/// Uses a lightweight `parking_lot::RwLock<Arc<T>>` internally: reads take
/// a short read lock and clone the `Arc`, writes take a short write lock
/// and swap the `Arc`. Best for very high read frequency with rare writes.
///
/// # Caution
///
/// Multiple concurrent writes may "lose" updates. Use when:
/// - Writes are rare
/// - Lost updates are acceptable
/// - Or use compare_exchange pattern
///
/// # Example
///
/// ```rust,ignore
/// use armature_core::cow_state::AtomicState;
/// use std::sync::Arc;
///
/// let state = AtomicState::new(Arc::new(Config::default()));
///
/// // Very fast reads
/// let config = state.load();
///
/// // Swap in a new value
/// state.store(Arc::new(new_config));
/// ```
pub struct AtomicState<T> {
    /// Current value
    current: parking_lot::RwLock<Arc<T>>,
    /// Version counter
    version: AtomicU64,
}

impl<T> AtomicState<T> {
    /// Create new atomic state.
    pub fn new(value: Arc<T>) -> Self {
        COW_STATS.record_state_created();
        Self {
            current: parking_lot::RwLock::new(value),
            version: AtomicU64::new(1),
        }
    }

    /// Load current value.
    ///
    /// Very fast - short read lock and Arc clone.
    #[inline]
    pub fn load(&self) -> Arc<T> {
        let arc = Arc::clone(&self.current.read());
        COW_STATS.record_snapshot_taken();
        arc
    }

    /// Store new value.
    ///
    /// Swaps in the new value, dropping the old once all readers release it.
    pub fn store(&self, value: Arc<T>) {
        *self.current.write() = value;
        self.version.fetch_add(1, Ordering::Release);
        COW_STATS.record_update();
    }

    /// Get current version.
    #[inline]
    pub fn version(&self) -> u64 {
        self.version.load(Ordering::Acquire)
    }

    /// Load with version.
    #[inline]
    pub fn load_versioned(&self) -> (Arc<T>, u64) {
        let value = self.load();
        let version = self.version();
        (value, version)
    }
}

impl<T: Clone> AtomicState<T> {
    /// Update using function (read-modify-write).
    ///
    /// Note: Not atomic - another update may happen between read and write.
    pub fn update<F>(&self, f: F)
    where
        F: FnOnce(&T) -> T,
    {
        let current = self.load();
        let new_value = f(&current);
        self.store(Arc::new(new_value));
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for AtomicState<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = self.load();
        f.debug_struct("AtomicState")
            .field("value", &*value)
            .field("version", &self.version())
            .finish()
    }
}

// ============================================================================
// Cached Value with Expiry
// ============================================================================

/// A cached value that automatically invalidates.
///
/// Useful for caching expensive computations with a TTL.
///
/// # Example
///
/// ```rust,ignore
/// use armature_core::cow_state::CachedValue;
/// use std::time::Duration;
///
/// let cache: CachedValue<Vec<User>> = CachedValue::new(Duration::from_secs(60));
///
/// // Get or compute
/// let users = cache.get_or_compute(|| {
///     db.query_param("SELECT * FROM users")
/// });
/// ```
pub struct CachedValue<T> {
    /// Cached value
    value: RwLock<Option<CacheEntry<T>>>,
    /// TTL
    ttl: std::time::Duration,
}

#[derive(Clone)]
struct CacheEntry<T> {
    value: Arc<T>,
    created_at: std::time::Instant,
    version: u64,
}

impl<T> CachedValue<T> {
    /// Create new cached value with TTL.
    pub fn new(ttl: std::time::Duration) -> Self {
        Self {
            value: RwLock::new(None),
            ttl,
        }
    }

    /// Check if cache is valid.
    pub fn is_valid(&self) -> bool {
        let guard = self.value.read().unwrap();
        guard
            .as_ref()
            .map(|e| e.created_at.elapsed() < self.ttl)
            .unwrap_or(false)
    }

    /// Get cached value if valid.
    pub fn get(&self) -> Option<Arc<T>> {
        let guard = self.value.read().unwrap();
        guard.as_ref().and_then(|e| {
            if e.created_at.elapsed() < self.ttl {
                COW_STATS.record_cache_hit();
                Some(Arc::clone(&e.value))
            } else {
                COW_STATS.record_cache_miss();
                None
            }
        })
    }

    /// Set cached value.
    pub fn set(&self, value: T) {
        let mut guard = self.value.write().unwrap();
        let version = guard.as_ref().map(|e| e.version + 1).unwrap_or(1);
        *guard = Some(CacheEntry {
            value: Arc::new(value),
            created_at: std::time::Instant::now(),
            version,
        });
    }

    /// Invalidate cache.
    pub fn invalidate(&self) {
        let mut guard = self.value.write().unwrap();
        *guard = None;
    }

    /// Get TTL.
    pub fn ttl(&self) -> std::time::Duration {
        self.ttl
    }

    /// Get remaining TTL.
    pub fn remaining_ttl(&self) -> Option<std::time::Duration> {
        let guard = self.value.read().unwrap();
        guard.as_ref().and_then(|e| {
            let elapsed = e.created_at.elapsed();
            if elapsed < self.ttl {
                Some(self.ttl - elapsed)
            } else {
                None
            }
        })
    }
}

impl<T: Clone> CachedValue<T> {
    /// Get cached value or compute it.
    pub fn get_or_compute<F>(&self, f: F) -> Arc<T>
    where
        F: FnOnce() -> T,
    {
        // Fast path: check read lock first
        {
            let guard = self.value.read().unwrap();
            if let Some(ref entry) = *guard
                && entry.created_at.elapsed() < self.ttl
            {
                COW_STATS.record_cache_hit();
                return Arc::clone(&entry.value);
            }
        }

        // Slow path: compute and store
        COW_STATS.record_cache_miss();
        let mut guard = self.value.write().unwrap();

        // Double-check after acquiring write lock
        if let Some(ref entry) = *guard
            && entry.created_at.elapsed() < self.ttl
        {
            return Arc::clone(&entry.value);
        }

        let value = Arc::new(f());
        let version = guard.as_ref().map(|e| e.version + 1).unwrap_or(1);
        *guard = Some(CacheEntry {
            value: Arc::clone(&value),
            created_at: std::time::Instant::now(),
            version,
        });
        value
    }

    /// Get cached value or compute it asynchronously.
    pub async fn get_or_compute_async<F, Fut>(&self, f: F) -> Arc<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = T>,
    {
        // Fast path
        {
            let guard = self.value.read().unwrap();
            if let Some(ref entry) = *guard
                && entry.created_at.elapsed() < self.ttl
            {
                COW_STATS.record_cache_hit();
                return Arc::clone(&entry.value);
            }
        }

        // Slow path
        COW_STATS.record_cache_miss();
        let value = Arc::new(f().await);

        let mut guard = self.value.write().unwrap();
        let version = guard.as_ref().map(|e| e.version + 1).unwrap_or(1);
        *guard = Some(CacheEntry {
            value: Arc::clone(&value),
            created_at: std::time::Instant::now(),
            version,
        });
        value
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for CachedValue<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let is_valid = self.is_valid();
        let remaining = self.remaining_ttl();
        f.debug_struct("CachedValue")
            .field("is_valid", &is_valid)
            .field("remaining_ttl", &remaining)
            .field("ttl", &self.ttl)
            .finish()
    }
}

// ============================================================================
// Global Statistics
// ============================================================================

/// Statistics for CoW state operations.
#[derive(Debug, Default)]
pub struct CowStats {
    /// States created
    states_created: AtomicU64,
    /// Snapshots taken
    snapshots_taken: AtomicU64,
    /// Snapshot clones
    snapshot_clones: AtomicU64,
    /// Updates performed
    updates: AtomicU64,
    /// Cache hits
    cache_hits: AtomicU64,
    /// Cache misses
    cache_misses: AtomicU64,
}

impl CowStats {
    fn record_state_created(&self) {
        self.states_created.fetch_add(1, Ordering::Relaxed);
    }

    fn record_snapshot_taken(&self) {
        self.snapshots_taken.fetch_add(1, Ordering::Relaxed);
    }

    fn record_snapshot_clone(&self) {
        self.snapshot_clones.fetch_add(1, Ordering::Relaxed);
    }

    fn record_update(&self) {
        self.updates.fetch_add(1, Ordering::Relaxed);
    }

    fn record_cache_hit(&self) {
        self.cache_hits.fetch_add(1, Ordering::Relaxed);
    }

    fn record_cache_miss(&self) {
        self.cache_misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get states created count.
    pub fn states_created(&self) -> u64 {
        self.states_created.load(Ordering::Relaxed)
    }

    /// Get snapshots taken count.
    pub fn snapshots_taken(&self) -> u64 {
        self.snapshots_taken.load(Ordering::Relaxed)
    }

    /// Get snapshot clones count.
    pub fn snapshot_clones(&self) -> u64 {
        self.snapshot_clones.load(Ordering::Relaxed)
    }

    /// Get updates count.
    pub fn updates(&self) -> u64 {
        self.updates.load(Ordering::Relaxed)
    }

    /// Get cache hits.
    pub fn cache_hits(&self) -> u64 {
        self.cache_hits.load(Ordering::Relaxed)
    }

    /// Get cache misses.
    pub fn cache_misses(&self) -> u64 {
        self.cache_misses.load(Ordering::Relaxed)
    }

    /// Get cache hit ratio.
    pub fn cache_hit_ratio(&self) -> f64 {
        let hits = self.cache_hits() as f64;
        let total = hits + self.cache_misses() as f64;
        if total > 0.0 { hits / total } else { 0.0 }
    }

    /// Get read/write ratio.
    pub fn read_write_ratio(&self) -> f64 {
        let reads = self.snapshots_taken() as f64;
        let writes = self.updates() as f64;
        if writes > 0.0 {
            reads / writes
        } else {
            f64::INFINITY
        }
    }
}

/// Global CoW statistics.
static COW_STATS: CowStats = CowStats {
    states_created: AtomicU64::new(0),
    snapshots_taken: AtomicU64::new(0),
    snapshot_clones: AtomicU64::new(0),
    updates: AtomicU64::new(0),
    cache_hits: AtomicU64::new(0),
    cache_misses: AtomicU64::new(0),
};

/// Get global CoW statistics.
pub fn cow_stats() -> &'static CowStats {
    &COW_STATS
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_snapshot_basic() {
        let snap = Snapshot::new(42);
        assert_eq!(*snap, 42);
        assert_eq!(snap.version(), 0);
    }

    #[test]
    fn test_snapshot_clone_is_cheap() {
        let snap = Snapshot::new(vec![1, 2, 3, 4, 5]);
        assert_eq!(snap.ref_count(), 1);

        let snap2 = snap.clone();
        assert_eq!(snap.ref_count(), 2);
        assert_eq!(snap2.ref_count(), 2);

        assert!(snap.same_source(&snap2));
    }

    #[test]
    fn test_cow_state_basic() {
        let state = CowState::new(10);
        assert_eq!(*state.snapshot(), 10);
        assert_eq!(state.version(), 1);
    }

    #[test]
    fn test_cow_state_update() {
        let state = CowState::new(vec![1, 2, 3]);
        let snap1 = state.snapshot();
        assert_eq!(state.version(), 1);

        state.update(|v| v.push(4));
        assert_eq!(state.version(), 2);

        let snap2 = state.snapshot();

        // Old snapshot unchanged
        assert_eq!(*snap1, vec![1, 2, 3]);
        // New snapshot has update
        assert_eq!(*snap2, vec![1, 2, 3, 4]);
    }

    #[test]
    fn test_cow_state_replace() {
        let state = CowState::new("old".to_string());
        state.replace("new".to_string());
        assert_eq!(*state.snapshot(), "new");
    }

    #[test]
    fn test_cow_state_update_if() {
        let state = CowState::new(5);

        // Should update (predicate true)
        let updated = state.update_if(|&v| v < 10, |v| *v += 1);
        assert!(updated);
        assert_eq!(*state.snapshot(), 6);

        // Should not update (predicate false)
        let updated = state.update_if(|&v| v > 100, |v| *v += 1);
        assert!(!updated);
        assert_eq!(*state.snapshot(), 6);
    }

    #[test]
    fn test_cow_state_compare_and_swap() {
        let state = CowState::new(100);
        let version = state.version();

        // Should succeed
        let result = state.compare_and_swap(version, |v| *v += 1);
        assert!(result.is_ok());
        assert_eq!(*state.snapshot(), 101);

        // Should fail (wrong version)
        let result = state.compare_and_swap(version, |v| *v += 1);
        assert!(result.is_err());
    }

    #[test]
    fn test_versioned_state() {
        let state = VersionedState::new(vec!["a", "b"]);
        let v1 = state.version();

        state.write(|v| v.push("c"));
        let v2 = state.version();

        assert_ne!(v1, v2);
        assert!(state.changed_since(v1));
        assert!(!state.changed_since(v2));
    }

    #[test]
    fn test_atomic_state() {
        let state = AtomicState::new(Arc::new(42));
        assert_eq!(*state.load(), 42);

        state.store(Arc::new(100));
        assert_eq!(*state.load(), 100);
    }

    #[test]
    fn test_atomic_state_update() {
        let state = AtomicState::new(Arc::new(10));
        state.update(|&v| v * 2);
        assert_eq!(*state.load(), 20);
    }

    #[test]
    fn test_atomic_state_concurrent_load_store() {
        // Regression: the old AtomicPtr-based implementation freed the value
        // read by concurrent `load` calls immediately after `store`.
        let state = Arc::new(AtomicState::new(Arc::new(vec![0u64; 64])));

        let readers: Vec<_> = (0..4)
            .map(|_| {
                let state = Arc::clone(&state);
                std::thread::spawn(move || {
                    for _ in 0..1000 {
                        let value = state.load();
                        // Every element of a stored vec has the same value
                        let first = value[0];
                        assert!(value.iter().all(|&v| v == first));
                    }
                })
            })
            .collect();

        let writer = {
            let state = Arc::clone(&state);
            std::thread::spawn(move || {
                for i in 1..500u64 {
                    state.store(Arc::new(vec![i; 64]));
                }
            })
        };

        for reader in readers {
            reader.join().unwrap();
        }
        writer.join().unwrap();

        assert_eq!(state.load()[0], 499);
    }

    #[test]
    fn test_cached_value() {
        let cache: CachedValue<i32> = CachedValue::new(std::time::Duration::from_secs(60));

        assert!(!cache.is_valid());
        assert!(cache.get().is_none());

        cache.set(42);
        assert!(cache.is_valid());
        assert_eq!(*cache.get().unwrap(), 42);
    }

    #[test]
    fn test_cached_value_compute() {
        let cache: CachedValue<i32> = CachedValue::new(std::time::Duration::from_secs(60));
        let mut computed_count = 0;

        let value = cache.get_or_compute(|| {
            computed_count += 1;
            42
        });
        assert_eq!(*value, 42);

        // Second call should use cache
        let value2 = cache.get_or_compute(|| {
            computed_count += 1;
            99
        });
        assert_eq!(*value2, 42); // Still 42, not recomputed
    }

    #[test]
    fn test_cow_stats() {
        let stats = cow_stats();
        let _ = stats.states_created();
        let _ = stats.snapshots_taken();
        let _ = stats.updates();
        let _ = stats.cache_hit_ratio();
        let _ = stats.read_write_ratio();
    }
}