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:

Example.

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

Generic Parameters:

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<Context, T> PublicMutable<T, Context>

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

Initializes a new PublicMutable state variable.

This function is usually automatically called within the #[storage] macro. You typically don't need to call this directly when writing smart contracts.

Arguments

  • context - One of PublicContext/UtilityContext. The Context determines which methods of this struct will be made available to the calling smart contract function.
  • storage_slot - A unique identifier for this state variable within the contract. Usually, the #[storage] macro will determine an appropriate storage_slot automatically. A smart contract dev shouldn't have to worry about this, as it's managed behind the scenes.

docs:start:public_mutable_struct_new

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> HasStorageSlot<M> for PublicMutable<T, Context>
where T: Packable<N = M>

pub fn get_storage_slot(self) -> Field