Skip to main content

SnapshotRing

Struct SnapshotRing 

Source
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

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Number of snapshots currently stored (up to capacity).

Source

pub fn is_empty(&self) -> bool

Whether the ring is empty.

Source

pub fn capacity(&self) -> usize

The ring buffer capacity.

Source

pub fn write_pos(&self) -> u64

The current monotonic write position.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.