pub struct Playback { /* private fields */ }Expand description
Immutable shared tape plus a cursor. Seeking restores the nearest earlier checkpoint once, then advances at most the caller’s work budget per pump. Verification stops at the first divergence. A seek verifies only the restored checkpoint and subsequent steps; start from the beginning to check every tick.
use ferravex_play::replay::{
EventIter, Header, Limits, Playback, PlaybackStatus, Recorder,
ReplayChecksum, ReplayWorld, input_checksum,
};
use std::sync::Arc;
struct Counter(u64);
impl ReplayWorld for Counter {
type Error = &'static str;
const SCHEMA: u64 = 1;
const VERSION: u32 = 1;
const TICK_HZ: u16 = 60;
fn context(&self) -> Result<u64, Self::Error> { Ok(0) }
fn restore(&mut self, state: &[u8]) -> Result<(), Self::Error> {
self.0 = u64::from_le_bytes(state.try_into().map_err(|_| "bad state")?);
Ok(())
}
fn step(&mut self, input: &[u8], _: EventIter<'_>) -> Result<(), Self::Error> {
self.0 += u64::from(*input.first().ok_or("missing input")?);
Ok(())
}
fn state_hash(&self) -> Result<u64, Self::Error> { Ok(self.0) }
}
let header = Header { schema: 1, version: 1, context: 0, tick_hz: 60, seed: 0 };
let mut recorder = Recorder::new(header, Limits::default(), 0, &0u64.to_le_bytes(), 0)?;
recorder.push(1, &[2], &[], None, 2)?;
recorder.push(2, &[3], &[], None, 5)?;
let mut playback = Playback::new(Arc::new(recorder.finish()));
let mut world = Counter(0);
playback.start(&mut world).unwrap();
playback.step_with_input(&mut world, &[2]).unwrap();
assert!(playback.step_with_input(&mut world, &[4]).is_err());
let mismatch = playback.first_mismatch().unwrap();
assert_eq!((mismatch.tick, mismatch.checksum), (2, ReplayChecksum::Input));
assert_eq!((mismatch.expected, mismatch.actual), (input_checksum(&[3]), input_checksum(&[4])));
assert_eq!(world.0, 2); // Rejected input was never applied.
playback.reset();
assert_eq!(playback.status(), PlaybackStatus::NotStarted);
playback.start(&mut world).unwrap(); // Restore before stepping again.
playback.step(&mut world).unwrap();
playback.step(&mut world).unwrap();
assert_eq!(playback.status(), PlaybackStatus::Finished);Implementations§
Source§impl Playback
impl Playback
pub fn new(recording: Arc<Recording>) -> Playback
pub fn recording(&self) -> &Recording
pub fn position(&self) -> u64
pub fn seeking(&self) -> bool
Sourcepub fn finished(&self) -> bool
pub fn finished(&self) -> bool
True only after successful verification from the last restored checkpoint through the end. An empty recording finishes when its checkpoint verifies.
Sourcepub fn fault(&self) -> Option<(u64, u64, u64)>
pub fn fault(&self) -> Option<(u64, u64, u64)>
Compatibility tuple (tick, expected, actual); prefer Self::first_mismatch
when distinguishing state checksums from supplied input checksums.
Sourcepub fn first_mismatch(&self) -> Option<ReplayMismatch>
pub fn first_mismatch(&self) -> Option<ReplayMismatch>
First mismatch in the current verification segment, retained across repeated step attempts. Start/seek begin a new segment after validation; reset clears it immediately. Seeking does not verify skipped ticks.
Sourcepub fn status(&self) -> PlaybackStatus
pub fn status(&self) -> PlaybackStatus
Inspect playback without treating an unstarted or faulted cursor as done.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Clear the cursor, pending seek and diagnostics without modifying the world.
Self::start must restore the world before another step is accepted.
The recording remains shared and no buffers are allocated.
Sourcepub fn start<W>(
&mut self,
world: &mut W,
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
pub fn start<W>(
&mut self,
world: &mut W,
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
Start or restart from the initial checkpoint. An incompatible world leaves the cursor and diagnostics unchanged. A restore/hash failure faults playback.
pub fn step<W>(
&mut self,
world: &mut W,
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
Sourcepub fn step_with_input<W>(
&mut self,
world: &mut W,
input: &[u8],
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
pub fn step_with_input<W>(
&mut self,
world: &mut W,
input: &[u8],
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
Compare externally sampled input with the next recorded input before
applying that tick, then verify the resulting state as Self::step does.
Byte equality is checked (not just checksum equality). On disagreement,
Self::first_mismatch records both input checksums and the upcoming tick;
neither the world nor cursor advances. Repeated calls return the first
mismatch until start, seek or reset. During a seek, returns Seeking.
See Playback for a complete example.
Sourcepub fn seek<W>(
&mut self,
world: &mut W,
target: u64,
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
pub fn seek<W>(
&mut self,
world: &mut W,
target: u64,
) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
Restore the checkpoint at or before target, clearing previous faults.
Invalid targets change nothing. The remaining prefix is verified by
Self::pump_seek; the skipped prefix is not checked for divergence.
Sourcepub fn pump_seek<W>(
&mut self,
world: &mut W,
budget: usize,
) -> Result<usize, PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
pub fn pump_seek<W>(
&mut self,
world: &mut W,
budget: usize,
) -> Result<usize, PlaybackError<<W as ReplayWorld>::Error>>where
W: ReplayWorld,
Verify up to budget ticks toward the pending seek target. A zero budget
preserves a pending seek. With no pending seek, returns zero (including
before start or after a failed seek); inspect Self::status for faults.