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>
impl<T> SnapshotRing<T>
pub fn new(capacity: usize) -> 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>
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)>)
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>
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)
pub fn truncate_after(&mut self, tick: u64)
Discard the future so a rollback host can append a new timeline.