Skip to main content

apply_queue

Function apply_queue 

Source
pub fn apply_queue(
    base: &mut Vec<u8>,
    delta: &ArchivedQueueDiff,
    item_ssz_size: usize,
)
Expand description

Applies a queue delta to a serialized SSZ queue in place.

For QueueDiff::Fifo, the specified number of items are removed from the front of base, after which the appended serialized items are added to the back.

For QueueDiff::FullReplacement, the existing queue is cleared and replaced with the serialized target queue stored in the delta.

§Arguments

  • base - Mutable serialized SSZ representation of the queue to update.
  • delta - Archived queue delta previously produced by diff_queue and serialized with rkyv.
  • item_ssz_size - Fixed serialized SSZ size of one queue item.

§Behavior

After successful execution, base represents the target queue from which the delta was originally generated.

§Panics

Panics if the archived delta cannot be deserialized.

§Complexity

For QueueDiff::Fifo, the operation is O(n) in the number of bytes removed and appended. Removing bytes from the front may require shifting the remaining contents of the Vec.

For QueueDiff::FullReplacement, the operation is O(n) in the size of the replacement queue.

§Example


const ITEM_SIZE: usize = 4;

let mut base = b"AAAABBBBCCCC".to_vec();
let target = b"CCCCDDDDEEEE";

let delta = diff_queue(&base, target, ITEM_SIZE);
let archived = rkyv::to_bytes::<rkyv::rancor::Error>(&delta).unwrap();
let archived = unsafe { rkyv::access_unchecked::<
    eth_state_diff::types::ArchivedQueueDiff
>(&archived) };

apply_queue(&mut base, archived, ITEM_SIZE);

assert_eq!(base, target);