Struct SnapshotRing

pub struct SnapshotRing<T> { /* private fields */ }
Expand description

Bounded history of strictly increasing, nonwrapping u64 ticks.

Duplicate or decreasing ticks are rejected; rollback hosts can explicitly discard future snapshots with truncate_after.

Implementations§

§

impl<T> SnapshotRing<T>

pub fn new(capacity: usize) -> SnapshotRing<T>

Create an empty history. A zero-capacity ring rejects every insertion.

pub fn push(&mut self, tick: u64, state: T) -> Result<Option<(u64, T)>, T>

Append a strictly newer tick, returning the oldest snapshot if evicted.

Zero capacity, duplicate ticks and decreasing ticks return Err(state) without changing history or evicting anything. Ticks do not wrap at u64::MAX; start a new history for a new tick epoch.

pub fn get(&self, tick: u64) -> Option<&T>

pub fn bracket(&self, tick: u64) -> (Option<(u64, &T)>, Option<(u64, &T)>)

Nearest samples on either side (inclusive); exact matches appear in both slots.

See bracket_checked for fractional queries, explicit boundary outcomes and a checked interpolation factor.

pub fn bracket_checked( &self, tick: u64, fraction: f64, ) -> Result<SnapshotBracket<'_, T>, SnapshotQueryError>

Locate tick + fraction without extrapolation or state allocation.

fraction must be finite and in [0, 1]; exactly 1 is normalized to the next whole tick. Integer ordering is preserved across the full u64 tick range: only relative tick differences are converted to f64 for the interpolation factor. A positive fraction at the newest tick returns AfterNewest, even at u64::MAX when the fraction is less than 1.

Exact queries return a single snapshot, so duplicates never create a zero-length interpolation interval. Inserts already reject duplicate and decreasing ticks. Lookup uses logical deque indices, works across wrapped storage, takes O(log(len)) time and does not mutate history.

§Errors

Invalid fractions return SnapshotQueryError::InvalidFraction. A query of (u64::MAX, 1.0) returns SnapshotQueryError::TickOverflow. Inputs are validated even when the ring is empty. No invalid input is clamped.

use ferravex_engine::snapshots::{SnapshotBracket, SnapshotQueryError, SnapshotRing};

let mut history = SnapshotRing::new(2);
history.push(10, "previous").unwrap();
history.push(12, "current").unwrap();
assert!(matches!(history.bracket_checked(9, 0.5)?,
    SnapshotBracket::BeforeOldest { oldest: (10, _) }));
assert!(matches!(history.bracket_checked(11, 1.0)?,
    SnapshotBracket::Exact { snapshot: (12, _) }));
assert!(matches!(history.bracket_checked(12, 0.5)?,
    SnapshotBracket::AfterNewest { newest: (12, _) }));
assert_eq!(history.bracket_checked(11, f64::NAN),
    Err(SnapshotQueryError::InvalidFraction));

pub fn truncate_after(&mut self, tick: u64)

Discard the future so a rollback host can append a new timeline.

pub fn len(&self) -> usize

pub fn is_empty(&self) -> bool

Auto Trait Implementations§

§

impl<T> Freeze for SnapshotRing<T>

§

impl<T> RefUnwindSafe for SnapshotRing<T>
where T: RefUnwindSafe,

§

impl<T> Send for SnapshotRing<T>
where T: Send,

§

impl<T> Sync for SnapshotRing<T>
where T: Sync,

§

impl<T> Unpin for SnapshotRing<T>
where T: Unpin,

§

impl<T> UnwindSafe for SnapshotRing<T>
where T: UnwindSafe,

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.