Skip to main content

ListMutTarget

Trait ListMutTarget 

Source
pub trait ListMutTarget<T: Copy> {
    // Required methods
    fn len(&self) -> usize;
    fn get_mut(&mut self, index: usize) -> Option<&mut T>;
    fn push(&mut self, value: T);

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

A mutable target for list-like collections of copyable values.

ListMutTarget provides the minimal interface required by the generic delta-application routines in this crate. It allows those routines to update consensus-state collections without requiring the collection to be backed by a contiguous Vec.

Implementations may use any underlying storage strategy, including contiguous buffers, persistent trees, or other client-specific data structures.

§Type parameter

T is the element type stored by the collection. It must implement Copy because delta application reads values from the encoded delta and writes them directly into the target collection.

§Required operations

An implementation must provide:

  • len to report the current number of elements.
  • get_mut to obtain mutable access to an existing element by index.
  • push to append a newly decoded element.

§Example

The crate provides an implementation for Vec<u64> and Vec<u8>.

use eth_state_diff::ListMutTarget;

let mut values = vec![100u64, 200, 300];
let target: &mut dyn ListMutTarget<u64> = &mut values;

*target.get_mut(1).unwrap() = 250;
target.push(400);

assert_eq!(values, [100, 250, 300, 400]);

§Implementing for client-specific collections

Consensus clients with non-contiguous or tree-backed state can implement this trait to allow the generic delta algorithms to operate directly on their native collections, without first materializing the collection as a flat buffer.

Implementations should return None from get_mut when the requested index is outside the current collection bounds.

Required Methods§

Source

fn len(&self) -> usize

Returns the current number of elements in the collection.

Source

fn get_mut(&mut self, index: usize) -> Option<&mut T>

Returns mutable access to the element at index.

Returns None if index is outside the current collection bounds.

Source

fn push(&mut self, value: T)

Appends value to the end of the collection.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ListMutTarget<u8> for Vec<u8>

Source§

fn len(&self) -> usize

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut u8>

Source§

fn push(&mut self, value: u8)

Source§

impl ListMutTarget<u64> for Vec<u64>

Source§

fn len(&self) -> usize

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut u64>

Source§

fn push(&mut self, value: u64)

Implementors§