vecdb/variants/base/
shared_len.rs

1//! Atomic length counter shared across clones.
2
3use std::sync::{
4    Arc,
5    atomic::{AtomicUsize, Ordering},
6};
7
8/// Atomic length counter shared across clones.
9///
10/// Wraps `Arc<AtomicUsize>` to allow multiple vec instances (original and clones)
11/// to observe the same length as the original grows.
12#[derive(Debug, Clone)]
13pub struct SharedLen(Arc<AtomicUsize>);
14
15impl SharedLen {
16    /// Creates a new shared length counter.
17    pub fn new(val: usize) -> Self {
18        Self(Arc::new(AtomicUsize::new(val)))
19    }
20
21    /// Gets the current length.
22    #[inline(always)]
23    pub fn get(&self) -> usize {
24        self.0.load(Ordering::SeqCst)
25    }
26
27    /// Sets the length.
28    #[inline]
29    pub fn set(&self, val: usize) {
30        self.0.store(val, Ordering::SeqCst);
31    }
32}
33
34impl Default for SharedLen {
35    fn default() -> Self {
36        Self::new(0)
37    }
38}