Struct EphemeralArray
pub struct EphemeralArray<T> {
pub slot: Field,
}
Fields
slot: FieldImplementations
impl<T> EphemeralArray<T>
pub unconstrained fn at(slot: Field) -> Self
Returns a handle to an ephemeral array at the given slot, which may already contain data (e.g. populated by an oracle).
pub unconstrained fn len(self) -> u32
Returns the number of elements stored in the array.
pub unconstrained fn push(self, value: T)
where
T: Serialize
Stores a value at the end of the array.
pub unconstrained fn pop(self) -> T
where
T: Deserialize
Removes and returns the last element. Panics if the array is empty.
pub unconstrained fn get(self, index: u32) -> T
where
T: Deserialize
Retrieves the value stored at index. Panics if the index is out of bounds.
pub unconstrained fn set(self, index: u32, value: T)
where
T: Serialize
Overwrites the value stored at index. Panics if the index is out of bounds.
pub unconstrained fn remove(self, index: u32)
Removes the element at index, shifting subsequent elements backward. Panics if out of bounds.
pub unconstrained fn clear(self) -> Self
Removes all elements from the array and returns self for chaining (e.g. EphemeralArray::at(slot).clear()
to get a guaranteed-empty array at a given slot).
pub unconstrained fn for_each<Env>(self, f: unconstrained fn[Env](u32, T))
where
T: Deserialize
Calls a function on each element of the array.
The function f is called once with each array value and its corresponding index. Iteration proceeds
backwards so that it is safe to remove the current element (and only the current element) inside the
callback.
It is not safe to push new elements from inside the callback.
A dynamically sized array that exists only during a single contract call frame.
Ephemeral arrays are backed by in-memory storage on the PXE side rather than a persistent database. Each contract call frame gets its own isolated slot space of ephemeral arrays. Child simulations cannot see the parent's ephemeral arrays, and vice versa.
Each logical array operation (push, pop, get, etc.) is a single oracle call, making ephemeral arrays significantly cheaper than capsule arrays.
Use Cases
Ephemeral arrays are designed for passing data between PXE (TypeScript) and contracts (Noir) during simulation, for example, note validation requests or event validation responses. This data type is appropriate for data that is not supposed to be persisted.
For data that needs to persist across simulations, contract calls, etc, use
CapsuleArrayinstead.