pub struct Delta {
pub base_tick: u32,
pub tick: u32,
pub bytes: Vec<u8>,
}Fields§
§base_tick: u32§tick: u32§bytes: Vec<u8>Implementations§
Source§impl Delta
impl Delta
Sourcepub fn encoded_len(base: &[u8], next: &[u8]) -> Result<usize, DeltaError>
pub fn encoded_len(base: &[u8], next: &[u8]) -> Result<usize, DeltaError>
Plan the exact inner wire length without allocating. Equal lengths and
the historical snapshot limit (MAX_PACKET_BYTES - 11) are checked before
counting changes, so all size arithmetic is bounded. This does not plan
the outer ServerExtension::SnapshotDelta envelope (24 more bytes).
use ferravex_net::delta::{Delta, DeltaError};
assert_eq!(Delta::encoded_len(&[0; 10], &[1; 10]), Ok(13));
assert_eq!(Delta::encoded_len(&[0; 10], &[0; 10]), Ok(3));
assert_eq!(Delta::encoded_len(&[0], &[]),
Err(DeltaError::LengthMismatch { base: 1, next: 0 }));Sourcepub fn encode_into(
base: &[u8],
next: &[u8],
destination: &mut [u8],
) -> Result<usize, DeltaError>
pub fn encode_into( base: &[u8], next: &[u8], destination: &mut [u8], ) -> Result<usize, DeltaError>
Encode just Delta::bytes into a caller slice, returning its written
prefix length. Snapshot validation precedes capacity validation. Every
error preserves the entire destination; success preserves the tail.
Uses the slice’s length, not any backing Vec’s capacity.
use ferravex_net::delta::Delta;
let mut output = [99; 20];
let n = Delta::encode_into(&[0; 10], &[1; 10], &mut output).unwrap();
assert_eq!(&output[..n], Delta::between(1, &[0; 10], 2, &[1; 10]).unwrap().bytes);
assert_eq!(&output[n..], &[99; 7]);Sourcepub fn between_into(
&mut self,
base_tick: u32,
base: &[u8],
tick: u32,
next: &[u8],
) -> Result<(), DeltaError>
pub fn between_into( &mut self, base_tick: u32, base: &[u8], tick: u32, next: &[u8], ) -> Result<(), DeltaError>
Replace this delta using its existing Vec capacity, with no allocation.
On error, ticks, bytes, length and capacity are unchanged. On success,
bytes are replaced (never appended); unused Vec capacity is not scrubbed.
Reserve outside the hot path. This deliberately allows any tick pair,
matching Self::between; the outer wire codec checks serial ordering.
use ferravex_net::delta::Delta;
let mut delta = Delta { base_tick: 0, tick: 0, bytes: Vec::with_capacity(20) };
delta.between_into(7, &[0; 10], 8, &[1; 10]).unwrap();
delta.between_into(8, &[1; 10], 9, &[1; 10]).unwrap();
assert_eq!(delta.bytes, [10, 0, 1]); // Dense storage was replaced by sparse.
assert_eq!(delta.apply(8, &[1; 10]).unwrap(), [1; 10]);pub fn between( base_tick: u32, base: &[u8], tick: u32, next: &[u8], ) -> Result<Delta, ProtocolError>
Sourcepub fn applied_len(
&self,
base_tick: u32,
base: &[u8],
) -> Result<usize, DeltaError>
pub fn applied_len( &self, base_tick: u32, base: &[u8], ) -> Result<usize, DeltaError>
Validate the entire delta and return the reconstructed snapshot length,
without allocation or writes. Checks run in this order: base tick,
header availability, base limit, encoded limit, encoded/base length,
tag/body shape, then each sparse range and order. No canonical sparse
density or value-change requirement is imposed, preserving legacy apply.
See the module’s runnable snapshot recipe and Self::apply_into.
Sourcepub fn apply_into(
&self,
base_tick: u32,
base: &[u8],
destination: &mut [u8],
) -> Result<usize, DeltaError>
pub fn apply_into( &self, base_tick: u32, base: &[u8], destination: &mut [u8], ) -> Result<usize, DeltaError>
Apply into a caller slice without allocating. Complete validation via
Self::applied_len precedes destination capacity checking and all writes.
Every error preserves the entire destination, even a late corrupt entry.
Success replaces only the returned prefix; the tail is untouched. Input
and destination must be disjoint. Matching ticks alone cannot verify the
baseline contents: select the exact retained bytes at the session layer.
use ferravex_net::delta::{Delta, DeltaError};
let delta = Delta::between(4, &[0; 10], 5, &[1; 10]).unwrap();
let mut output = [99; 12];
assert_eq!(delta.apply_into(3, &[0; 10], &mut output),
Err(DeltaError::BaseTickMismatch { expected: 4, actual: 3 }));
assert_eq!(output, [99; 12]);
assert_eq!(delta.apply_into(4, &[0; 10], &mut output), Ok(10));
assert_eq!(output, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 99, 99]);