Module delta

Source
Expand description

Versioned byte snapshot differences. Callers acknowledge exact baselines before using a delta.

The inner bytes remain [length: u16 LE, tag: u8, body...]: tag 0 stores the full snapshot, tag 1 stores ordered [index: u16 LE, value: u8] entries. Construction chooses sparse only when its body is strictly smaller. The checked buffer methods allocate nothing, validate before writing, and leave unused destination tails untouched. Safe Rust requires disjoint input and output borrows; these methods do not support overlapping/in-place application. No method authenticates a baseline’s contents, session, ACK or tick ordering.

This runnable acknowledged-base recipe uses the actual v5 snapshot, ACK and delta packet codecs. It models one already-installed baseline, not a retention or ACK-authorization policy. Outer packet codecs still allocate and enforce their own tighter size and tick-order limits; fall back to a full snapshot if delta construction or packet encoding fails in a real sender.

use ferravex_net::{
    ClientExtension, ClientPacket, ServerExtension, ServerPacket, Snapshot,
    MAX_PACKET_BYTES, decode_client, decode_server, encode_client, encode_server,
    delta::Delta,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let baseline = Snapshot { session: 7, tick: 40, ..Snapshot::default() };
    let base = encode_server(&ServerPacket::Snapshot(baseline.clone()))?;
    // The receiver validates and installs the full snapshot before ACKing it.
    assert_eq!(decode_server(&base)?, ServerPacket::Snapshot(baseline.clone()));
    let ack = encode_client(&ClientPacket::Extension(ClientExtension::SnapshotAck {
        session: baseline.session, tick: Some(baseline.tick),
    }))?;
    let ClientPacket::Extension(ClientExtension::SnapshotAck {
        session: 7, tick: Some(40),
    }) = decode_client(&ack)? else { panic!("unexpected ACK") };
    // The sender selects these exact retained bytes for that acknowledged tick.
    let mut next = baseline;
    next.tick = 41;
    let full = encode_server(&ServerPacket::Snapshot(next.clone()))?;
    let mut delta = Delta { base_tick: 0, tick: 0,
        bytes: Vec::with_capacity(MAX_PACKET_BYTES) };
    delta.between_into(40, &base, 41, &full)?;
    let packet = encode_server(&ServerPacket::Extension(
        ServerExtension::SnapshotDelta { session: 7, delta },
    ))?;
    let ServerPacket::Extension(ServerExtension::SnapshotDelta {
        session: 7, delta,
    }) = decode_server(&packet)? else { panic!("unexpected delta") };
    let mut output = [0; MAX_PACKET_BYTES];
    let len = delta.applied_len(40, &base)?;
    assert_eq!(delta.apply_into(40, &base, &mut output)?, len);
    let ServerPacket::Snapshot(recovered) = decode_server(&output[..len])?
        else { panic!("unexpected reconstructed packet") };
    assert_eq!(recovered.session, 7);
    assert_eq!(recovered.tick, delta.tick);
    assert_eq!(recovered, next); // Publish/ACK only after these checks.
    Ok(())
}

Structs§

Delta

Enums§

DeltaError
Exact rejection from the checked delta APIs. All indices are zero based and lengths/capacities are bytes. Legacy methods map every rejection to ProtocolError::Invalid.

Functions§

dequantize_position
quantize_position
Centimeter precision for a fixed world range; invalid or out-of-range values fail explicitly.