#[interned]Expand description
Defines an interned struct.
All fields jointly determine the struct’s identity. Within a revision, every occurrence of equal field values maps to the same compact handle. Interned fields are immutable.
The annotated item must be a struct with named fields. It may declare one lifetime parameter,
which Salsa treats as the database lifetime, but no type or const parameters. The generated
struct is Copy and provides a constructor and field getters. Every field type must implement
Clone + Eq + Hash + Send + Sync. A field whose type is unconditionally
'static is accepted directly; any other field must implement salsa::SalsaValue.
See interned structs in the salsa crate documentation for their identity and lifecycle.
§Options
Options are comma-separated inside the attribute:
constructor = IDENTrenames the generated constructor fromnewtoIDENT.debugimplementsDebugusing the field values when a database is attached to the current thread. The generateddefault_debug_fmtmethod can also be called from a manualDebugimplementation.revisions = EXPRsets the minimum number of active revisions an unused value is retained before its slot may be reused. The default is3. The value must be nonzero;usize::MAXdisables reuse.heap_size = PATHrecords heap use for Salsa’s unstable memory-usage reporting.PATHmust accept a reference to the tuple of all fields and return its heap allocation size in bytes.persistenables persistent caching when Salsa’spersistencefeature is enabled. Fields are serialized as a tuple withserdeby default.persist(serialize = PATH, deserialize = PATH)enables persistence with custom tuple serialization functions. Either path may be omitted to use the correspondingserdeimplementation.
§Legacy adapters
These options exist to adapt older code or external representations to Salsa. New code should
use the default lifetime-bearing struct and salsa::Id, and its field types should implement
salsa::SalsaValue.
id = PATHusesPATHas a legacy ID adapter instead ofsalsa::Id. The custom type must implementCopy+Clone+PartialEq+Eq+Hashas well assalsa::plumbing::AsIdandsalsa::plumbing::FromId.- Unsafe:
unsafe(no_lifetime)is strongly discouraged. It adapts code that cannot carry the database lifetime by generating a struct without one. This bypasses the compile-time guarantee that an interned handle cannot outlive its database. It must be combined withrevisions = usize::MAXso that interned slots are never reclaimed or reused. - Unsafe:
unsafe(non_salsa_values)is strongly discouraged. It adapts field types that do not implementsalsa::SalsaValueby suppressing the generated checks. The caller becomes responsible for ensuring retained values remain valid across revisions. Prefer adapting only the affected field with#[salsa_value(unsafe(prove_safe_to_retain_manually))].
§Field attributes
Every field generates a getter with the same name and visibility as the field. These helper attributes configure that getter:
#[returns(MODE)]selects how the getter returns the field.ref(the default) returns&FieldTy;clonereturns an ownedFieldTyusingClone;copyreturns an ownedFieldTyusingCopy; andderefusesDerefto return&<FieldTy as Deref>::Target.as_refandas_derefusesalsa::SalsaAsRefandsalsa::SalsaAsDerefto return borrowed forms such asOption<&T>andOption<&T::Target>. Every borrowed result is tied to the database borrow.#[get(IDENT)]renames the generated getter.- Unsafe:
#[salsa_value(unsafe(prove(Predicate, ...)))]replaces the retention check with predicates that must hold for every database lifetime. The compiler verifies the predicates, but the caller must ensure they imply that Salsa can retain the field and expose it with a later database lifetime. - Unsafe:
#[salsa_value(unsafe(prove_safe_to_retain_manually))]suppresses the retention check for this field without adding predicates. The caller must ensure Salsa can retain the field and expose it with a later database lifetime.
Other attributes, including documentation and lint attributes, are copied to the generated getter.
§Example
#[salsa::interned(debug)]
struct Name<'db> {
#[returns(deref)]
text: String,
#[returns(copy)]
#[get(disambiguator)]
index: u32,
}