alloy_eip7928/
slot_changes.rs

1//! Contains the [`SlotChanges`] struct, which represents all changes made to a single storage slot
2//! across
3
4use crate::StorageChange;
5use alloc::vec::Vec;
6use alloy_primitives::U256;
7
8/// Represents all changes made to a single storage slot across multiple transactions.
9#[derive(Debug, Clone, Default, PartialEq, Eq)]
10#[cfg_attr(feature = "rlp", derive(alloy_rlp::RlpEncodable, alloy_rlp::RlpDecodable))]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
13#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15pub struct SlotChanges {
16    /// The storage slot key being modified.
17    pub slot: U256,
18    /// A list of write operations to this slot, ordered by transaction index.
19    #[cfg_attr(feature = "serde", serde(alias = "slotChanges"))]
20    pub changes: Vec<StorageChange>,
21}
22
23impl SlotChanges {
24    /// Creates a new [`SlotChanges`] instance for the given slot key and changes.
25    ///
26    /// Preallocates capacity for up to 300,000 changes.
27    #[inline]
28    pub const fn new(slot: U256, changes: Vec<StorageChange>) -> Self {
29        Self { slot, changes }
30    }
31
32    /// Appends a storage change to the list.
33    #[inline]
34    pub fn push(&mut self, change: StorageChange) {
35        self.changes.push(change)
36    }
37
38    /// Returns `true` if no changes have been recorded.
39    #[inline]
40    pub const fn is_empty(&self) -> bool {
41        self.changes.is_empty()
42    }
43
44    /// Returns the number of changes recorded for this slot.
45    #[inline]
46    pub const fn len(&self) -> usize {
47        self.changes.len()
48    }
49
50    /// Creates a new `SlotChanges` for the given slot.
51    #[inline]
52    pub const fn with_slot(mut self, slot: U256) -> Self {
53        self.slot = slot;
54        self
55    }
56
57    /// Creates a new `SlotChanges` with the given change appended.
58    #[inline]
59    pub fn with_change(mut self, change: StorageChange) -> Self {
60        self.changes.push(change);
61        self
62    }
63}