aztec-nr - noir_aztec::unconstrained_array

Struct UnconstrainedArray

pub struct UnconstrainedArray<T, Oracle>
{ /* private fields */ }

A dynamically sized array backed by PXE-side in-memory storage via an [ArrayOracles] backend.

Arrays are identified by a slot, and each logical operation (push, pop, get, etc.) is a single oracle call. The Oracle backend determines the array's lifetime and visibility; contracts should not use this type directly but rather one of its aliases: EphemeralArray (scoped to a single contract call frame) or TransientArray (shared across all frames of the same contract within one top-level PXE call).

Implementations

impl<Oracle, T> UnconstrainedArray<T, Oracle>

pub unconstrained fn at(slot: Field) -> Self
where Oracle: ArrayOracles

Returns a handle to the array at the given slot, which may already contain data (e.g. populated by an oracle or by an earlier frame, depending on the backend's visibility).

pub unconstrained fn empty_at(slot: Field) -> Self
where Oracle: ArrayOracles

Returns an empty array at the given slot, clearing any pre-existing data.

For backends whose arrays are visible beyond a single call frame (e.g. transient arrays), this wipes data other frames of the same contract may have written at the slot.

pub unconstrained fn empty() -> Self
where Oracle: ArrayOracles

Returns an empty array at a fresh, randomly allocated slot.

Use this when the caller does not need a specific slot: the random slot is isolated from every other array of the same backend with overwhelming probability. Prefer UnconstrainedArray::empty_at when the slot must be a known value (e.g. one shared with an oracle or another call frame).

pub unconstrained fn len(self) -> u32
where Oracle: ArrayOracles

Returns the number of elements stored in the array.

pub unconstrained fn push(self, value: T)
where T: Serialize, Oracle: ArrayOracles

Stores a value at the end of the array.

pub unconstrained fn pop(self) -> T
where T: Deserialize, Oracle: ArrayOracles

Removes and returns the last element. Implementors are required to panic if the array is empty.

pub unconstrained fn get(self, index: u32) -> T
where T: Deserialize, Oracle: ArrayOracles

Retrieves the value stored at index. Implementors are required to panic if the index is out of bounds.

pub unconstrained fn set(self, index: u32, value: T)
where T: Serialize, Oracle: ArrayOracles

Overwrites the value stored at index. Implementors are required to panic if the index is out of bounds.

pub unconstrained fn remove(self, index: u32)
where Oracle: ArrayOracles

Removes the element at index, shifting subsequent elements backward. Implementors are required to panic if the index is out of bounds.

pub unconstrained fn clear(self) -> Self
where Oracle: ArrayOracles

Removes all elements from the array and returns self for chaining.

pub unconstrained fn for_each<Env>(self, f: unconstrained fn[Env](u32, T))
where T: Deserialize, Oracle: ArrayOracles

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.

pub unconstrained fn map<U, Env>( self, f: unconstrained fn[Env](T) -> U, ) -> UnconstrainedArray<U, Oracle>
where T: Deserialize, U: Serialize, Oracle: ArrayOracles

Applies f to every element and collects the results into a fresh array.

pub unconstrained fn filter<Env>(self, f: unconstrained fn[Env](T) -> bool) -> Self
where T: Serialize, T: Deserialize, Oracle: ArrayOracles

Collects every element satisfying the predicate f into a fresh array.

pub unconstrained fn any<Env>(self, f: unconstrained fn[Env](T) -> bool) -> bool
where T: Serialize, T: Deserialize, Oracle: ArrayOracles

Returns true if at least one element satisfies the predicate f.

pub unconstrained fn all<Env>(self, f: unconstrained fn[Env](T) -> bool) -> bool
where T: Serialize, T: Deserialize, Oracle: ArrayOracles

Returns true if every element satisfies the predicate f (vacuously true for an empty array).

pub unconstrained fn find<Env>(self, f: unconstrained fn[Env](T) -> bool) -> Option<T>
where T: Deserialize, Oracle: ArrayOracles

Returns the first element satisfying the predicate f, or Option::none if none do.

Trait implementations

impl<T> Deserialize for UnconstrainedArray<T, EphemeralOracles>

pub fn deserialize(fields: [Field; 1]) -> Self pub fn stream_deserialize<let K: u32>(reader: &mut Reader<K>) -> Self

impl<T> Serialize for UnconstrainedArray<T, EphemeralOracles>

pub fn serialize(self) -> [Field; 1] pub fn stream_serialize<let K: u32>(self, writer: &mut Writer<K>)