Module history

Source
Expand description

Bounded authority history and clock estimates; rendering and transport are independent.

History::bracket_precise keeps tick identity as an integer. Its optional freshness cutoff is caller data, independent of the requested presentation time. Here a host supplies both times in the same nonwrapping session epoch:

use ferravex_net::history::{History, HistoryBracket, HistoryEndpointPolicy};
use ferravex_net::clock::{ClientClock, TickUnwrapper};

#[derive(Debug, PartialEq)]
struct Pose { x: f64 }
let base = (1_u64 << 53) + 10;
let mut history = History::new(2);
assert!(history.push(base, Pose { x: 2.0 }));
assert!(history.push(base + 1, Pose { x: 6.0 }));
let now_tick = base + 2; // Supplied authority time, not a wall clock read.
let minimum_newest_tick = Some(now_tick.saturating_sub(2));
let query = |tick, fraction| history.bracket_precise(
    tick, fraction, HistoryEndpointPolicy::Reject, minimum_newest_tick);
assert!(matches!(query(base + 1, 0.0)?,
    HistoryBracket::Exact { sample: (tick, pose) }
        if tick == base + 1 && pose.x == 6.0));
let presented = match query(base, 0.25)? {
    HistoryBracket::Interpolated { before: (_, a), after: (_, b), alpha } => {
        Pose { x: a.x + (b.x - a.x) * alpha }
    }
    other => panic!("expected retained neighbors, got {other:?}"),
};
assert_eq!(presented, Pose { x: 3.0 });
assert!(history.push(base + 2, Pose { x: 10.0 })); // Evicts base.
assert!(matches!(history.bracket_precise(base, 0.5,
    HistoryEndpointPolicy::Reject, None)?, HistoryBracket::BeforeOldest { .. }));
assert!(matches!(history.bracket_precise(base, 0.5,
    HistoryEndpointPolicy::Clamp, None)?,
    HistoryBracket::Clamped { sample: (tick, _) } if tick == base + 1));
let later_now_tick = base + 6;
assert!(matches!(history.bracket_precise(base + 1, 0.0,
    HistoryEndpointPolicy::Clamp, Some(later_now_tick - 2))?,
    HistoryBracket::Stale { newest: (tick, _), .. } if tick == base + 2));

// At reconnect, first exclude old-session traffic at the transport boundary.
// Clear retained poses and reset the separate clock/tick epochs together.
let mut clock = ClientClock::default();
let mut ticks = TickUnwrapper::default();
ticks.observe(u32::MAX);
history.clear();
clock.reset();
ticks.reset();
assert_eq!(ticks.observe(1), Some(1));
assert!(history.push(1, Pose { x: 20.0 })); // Lower new-session ticks now fit.
assert_eq!(history.len(), 1);

Structs§

ClockSync
History

Enums§

HistoryBracket
Placement within actual retained samples; states are borrowed without cloning.
HistoryEndpointPolicy
Treatment of valid queries outside retained samples. Neither policy extrapolates.
HistoryQueryError
Invalid input to History::bracket_precise, validated even for empty history.