aztec-nr - noir_aztec::state_vars::public_mutable

Struct PublicMutable

pub struct PublicMutable<T, Context>
{ /* private fields */ }

PublicMutable

PublicMutable is a public state variable type for values that can be read and written within #[external("public")] functions of your smart contract.

You can declare a state variable of type PublicMutable within your contract's #[storage] struct:

E.g.: your_variable: PublicMutable<T, Context> or: your_mapping: Map<Field, PublicMutable<T, Context>>

The methods of PublicMutable are:

  • read
  • write (see the methods' own doc comments for more info).

Example.

A voting contract's proposal count can be represented as a PublicMutable. The count can be read by anyone to see how many proposals exist, and incremented when new proposals are submitted.

Generic Parameters:

  • T - The type of value stored (must implement Packable).
  • Context - The execution context (PublicContext or UtilityContext).

Advanced

Unlike private state variables which use notes, PublicMutable stores values directly in Aztec's public data tree. This enables direct read and write access to the current state during public function execution.

docs:start:public_mutable_struct

Implementations

impl<T> PublicMutable<T, UtilityContext>

pub unconstrained fn read(self) -> T
where T: Packable

Reads the current value stored in this PublicMutable state variable.

Notice that this function is executable only within a UtilityContext, which is an unconstrained environment on the user's local device.

Returns

  • T - The current value stored in this PublicMutable.

impl<T> PublicMutable<T, PublicContext>

pub fn read(self) -> T
where T: Packable

Reads the current value stored in this PublicMutable state variable.

Returns

  • T - The current value stored in this PublicMutable.

docs:start:public_mutable_struct_read

pub fn write(self, value: T)
where T: Packable

Writes a new value to this PublicMutable state variable.

Arguments

  • value - The new value to store in this PublicMutable.

Advanced

This function updates the value stored in Aztec's public data tree. The new value becomes immediately available to subsequent reads within the same transaction.

docs:start:public_mutable_struct_write

Trait implementations

impl<Context, let M: u32, T> StateVariable<M, Context> for PublicMutable<T, Context>
where T: Packable<N = M>

pub fn new(context: Context, storage_slot: Field) -> Self pub fn get_storage_slot(self) -> Field