Skip to main content

input

Attribute Macro input 

Source
#[input]
Expand description

Defines a mutable input to a Salsa database.

Each constructed input has a distinct identity that remains stable when its fields are changed. Reading a field records a dependency on that field; setting it invalidates queries that read it.

The macro replaces a named-field struct with a compact, Copy Salsa ID and generates a constructor, a builder, and getter and setter methods for every field.

See input structs in the salsa crate documentation for their identity, field-level dependencies, and lifecycle.

The annotated item must be a struct with named fields and no generic parameters.

§Options

Options are comma-separated inside the attribute:

  • constructor = IDENT renames the generated constructor from new to IDENT.
  • debug implements Debug using the field values when a database is attached to the current thread. The generated default_debug_fmt method can also be called from a manual Debug implementation.
  • singleton permits only one instance of this input type in a database and generates try_get(db) and get(db) methods for retrieving it.
  • heap_size = PATH records heap use for Salsa’s unstable memory-usage reporting. PATH must accept a reference to the tuple of all fields and return its heap allocation size in bytes.
  • persist enables persistent caching when Salsa’s persistence feature is enabled. Fields are serialized as a tuple with serde by default.
  • persist(serialize = PATH, deserialize = PATH) enables persistence with custom tuple serialization functions. Either path may be omitted to use the corresponding serde implementation.

§Field attributes

Every field generates getter and setter methods with the same name and visibility as the field. These helper attributes configure those methods:

  • #[returns(MODE)] selects how the getter returns the field. ref (the default) returns &FieldTy; clone returns an owned FieldTy using Clone; copy returns an owned FieldTy using Copy; and deref uses Deref to return &<FieldTy as Deref>::Target. as_ref and as_deref use salsa::SalsaAsRef and salsa::SalsaAsDeref to return borrowed forms such as Option<&T> and Option<&T::Target>. Every borrowed result is tied to the database borrow.
  • #[get(IDENT)] renames the generated getter.
  • #[set(IDENT)] renames the generated setter.
  • #[default] initializes the field with Default::default, omits it from the constructor’s arguments, and adds a builder method for overriding the default.

Input fields do not support #[salsa_value(...)]. Input field types are stored without database lifetime rebinding, so neither conditional nor unconditional retention proofs apply.

Other attributes, including documentation and lint attributes, are copied to the generated getter.