salsa/tests/manual_registration.rs
Ibraheem Ahmed dba66f1a37
Some checks failed
Book / Book (push) Has been cancelled
Release-plz / Release-plz release (push) Has been cancelled
Release-plz / Release-plz PR (push) Has been cancelled
Test / Test (push) Has been cancelled
Test / Miri (push) Has been cancelled
Test / Shuttle (push) Has been cancelled
Test / Benchmarks (push) Has been cancelled
Book / Deploy (push) Has been cancelled
Use inventory for static ingredient registration (#934)
* use `inventory` for static ingredient registration

* remove unnecessary synchronization from memo tables

* use global ingredient caches for database-independent ingredients

* add manual ingredient registration API

* remove static ingredient index optimization when manual registration is in use

* fix atomic imports

* simplify ingredient caches
2025-07-18 04:55:50 +00:00

92 lines
2.6 KiB
Rust

#![cfg(not(feature = "inventory"))]
mod ingredients {
#[salsa::input]
pub(super) struct MyInput {
field: u32,
}
#[salsa::tracked]
pub(super) struct MyTracked<'db> {
pub(super) field: u32,
}
#[salsa::interned]
pub(super) struct MyInterned<'db> {
pub(super) field: u32,
}
#[salsa::tracked]
pub(super) fn intern<'db>(db: &'db dyn salsa::Database, input: MyInput) -> MyInterned<'db> {
MyInterned::new(db, input.field(db))
}
#[salsa::tracked]
pub(super) fn track<'db>(db: &'db dyn salsa::Database, input: MyInput) -> MyTracked<'db> {
MyTracked::new(db, input.field(db))
}
}
#[salsa::db]
#[derive(Clone, Default)]
pub struct DatabaseImpl {
storage: salsa::Storage<Self>,
}
#[salsa::db]
impl salsa::Database for DatabaseImpl {}
#[test]
fn single_database() {
let db = DatabaseImpl {
storage: salsa::Storage::builder()
.ingredient::<ingredients::track>()
.ingredient::<ingredients::intern>()
.ingredient::<ingredients::MyInput>()
.ingredient::<ingredients::MyTracked<'_>>()
.ingredient::<ingredients::MyInterned<'_>>()
.build(),
};
let input = ingredients::MyInput::new(&db, 1);
let tracked = ingredients::track(&db, input);
let interned = ingredients::intern(&db, input);
assert_eq!(tracked.field(&db), 1);
assert_eq!(interned.field(&db), 1);
}
#[test]
fn multiple_databases() {
let db1 = DatabaseImpl {
storage: salsa::Storage::builder()
.ingredient::<ingredients::intern>()
.ingredient::<ingredients::MyInput>()
.ingredient::<ingredients::MyInterned<'_>>()
.build(),
};
let input = ingredients::MyInput::new(&db1, 1);
let interned = ingredients::intern(&db1, input);
assert_eq!(interned.field(&db1), 1);
// Create a second database with different ingredient indices.
let db2 = DatabaseImpl {
storage: salsa::Storage::builder()
.ingredient::<ingredients::track>()
.ingredient::<ingredients::intern>()
.ingredient::<ingredients::MyInput>()
.ingredient::<ingredients::MyTracked<'_>>()
.ingredient::<ingredients::MyInterned<'_>>()
.build(),
};
let input = ingredients::MyInput::new(&db2, 2);
let interned = ingredients::intern(&db2, input);
assert_eq!(interned.field(&db2), 2);
let input = ingredients::MyInput::new(&db2, 3);
let tracked = ingredients::track(&db2, input);
assert_eq!(tracked.field(&db2), 3);
}