Struct Playback

Source
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

Source

pub fn new(recording: Arc<Recording>) -> Playback

Source

pub fn recording(&self) -> &Recording

Source

pub fn position(&self) -> u64

Source

pub fn seeking(&self) -> bool

Source

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.

Source

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.

Source

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.

Source

pub fn status(&self) -> PlaybackStatus

Inspect playback without treating an unstarted or faulted cursor as done.

Source

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.

Source

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.

Source

pub fn step<W>( &mut self, world: &mut W, ) -> Result<(), PlaybackError<<W as ReplayWorld>::Error>>
where W: ReplayWorld,

Source

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.

Source

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.

Source

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.

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.