Expand description
OpenZeppelin’s EnumerableSet implementation for EVM storage using Rust primitives. https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/structs/EnumerableSet.sol
§Storage Layout
EnumerableSet uses two storage structures:
- Values Vec: A
Vec<T>storing all set elements atkeccak256(base_slot) - Positions Mapping: A
Mapping<T, u32>atbase_slot + 1storing 1-indexed positions- Position 0 means the value is not in the set
- Position N means the value is at index N-1 in the values array
§Design
Two complementary types:
Set<T>: Read-only in-memory snapshot.Vec<T>wrapper. Ordered like storage.SetHandler<T>: Storage operations.
§Usage Patterns
§Single Operations (O(1) each)
ⓘ
handler.insert(addr)?; // Direct storage write
handler.remove(&addr)?; // Direct storage write
handler.contains(&addr)?; // Direct storage read§Bulk Read
ⓘ
let set: Set<Address> = handler.read()?;
for addr in &set {
// Iteration preserves storage order
// set[i] == handler.at(i)
}§Bulk Mutation
ⓘ
let mut vec: Vec<_> = handler.read()?.into();
vec.push(new_addr);
vec.retain(|a| a != &old_addr);
handler.write(vec.into())?; // `Set::from(vec)` deduplicatesStructs§
- Set
- An ordered set that preserves insertion order.
- SetHandler
- Type-safe handler for accessing
Set<T>in storage.