pub struct SnapshotRing { /* private fields */ }Expand description
A fixed-capacity ring buffer of Arc<OwnedSnapshot>.
Single-producer: only one thread calls push. Multi-consumer: any
thread can call latest or get_by_pos to read snapshots.
The write position is monotonically increasing (never wraps). Slot
index is computed as pos % capacity. Each slot stores a position
tag alongside the snapshot so that consumers can verify they are
reading the slot they intended, even under concurrent producer pushes.
Implementations§
Source§impl SnapshotRing
impl SnapshotRing
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new ring buffer with the given capacity.
§Panics
Panics if capacity < 2. A ring buffer needs at least 2 slots
to be useful (one being written, one readable).
Sourcepub fn push(&self, snapshot: OwnedSnapshot) -> Option<Arc<OwnedSnapshot>>
pub fn push(&self, snapshot: OwnedSnapshot) -> Option<Arc<OwnedSnapshot>>
Push a new snapshot into the ring. Single-producer only.
Returns the evicted snapshot (if any) that was displaced by this push. The caller can use this for reclamation bookkeeping.
Sourcepub fn latest(&self) -> Option<Arc<OwnedSnapshot>>
pub fn latest(&self) -> Option<Arc<OwnedSnapshot>>
Get the latest (most recently pushed) snapshot.
Returns None only if no snapshots have been pushed yet.
On overwrite races (producer wraps the slot between our
write_pos read and lock acquisition), retries from the
fresh write_pos to guarantee returning an available
snapshot whenever the ring is non-empty.
Sourcepub fn get_by_pos(&self, pos: u64) -> Option<Arc<OwnedSnapshot>>
pub fn get_by_pos(&self, pos: u64) -> Option<Arc<OwnedSnapshot>>
Get a snapshot by its monotonic write position.
Returns None if the position has been evicted (overwritten) or
hasn’t been written yet.