diff --git a/components/salsa-2022-macros/src/tracked_fn.rs b/components/salsa-2022-macros/src/tracked_fn.rs index d5bcb940..01f6ebe4 100644 --- a/components/salsa-2022-macros/src/tracked_fn.rs +++ b/components/salsa-2022-macros/src/tracked_fn.rs @@ -230,6 +230,48 @@ fn ingredients_for_impl( } }; + let function: syn::Expr = if let Some(lru) = args.lru.clone() { + parse_quote! { + { + let index = routes.push( + |jars| { + let jar = >::jar_from_jars(jars); + let ingredients = + <_ as salsa::storage::HasIngredientsFor>::ingredient(jar); + &ingredients.function + }, + |jars| { + let jar = >::jar_from_jars_mut(jars); + let ingredients = + <_ as salsa::storage::HasIngredientsFor>::ingredient_mut(jar); + &mut ingredients.function + }); + let ingredient = salsa::function::FunctionIngredient::new(index); + ingredient.set_capacity(#lru); + ingredient + } + } + } else { + parse_quote! { + { + let index = routes.push( + |jars| { + let jar = >::jar_from_jars(jars); + let ingredients = + <_ as salsa::storage::HasIngredientsFor>::ingredient(jar); + &ingredients.function + }, + |jars| { + let jar = >::jar_from_jars_mut(jars); + let ingredients = + <_ as salsa::storage::HasIngredientsFor>::ingredient_mut(jar); + &mut ingredients.function + }); + salsa::function::FunctionIngredient::new(index) + } + } + }; + parse_quote! { impl salsa::storage::IngredientsFor for #config_ty { type Ingredients = Self; @@ -242,22 +284,7 @@ fn ingredients_for_impl( Self { intern_map: #intern_map, - function: { - let index = routes.push( - |jars| { - let jar = >::jar_from_jars(jars); - let ingredients = - <_ as salsa::storage::HasIngredientsFor>::ingredient(jar); - &ingredients.function - }, - |jars| { - let jar = >::jar_from_jars_mut(jars); - let ingredients = - <_ as salsa::storage::HasIngredientsFor>::ingredient_mut(jar); - &mut ingredients.function - }); - salsa::function::FunctionIngredient::new(index) - }, + function: #function } } } diff --git a/components/salsa-2022/src/function/lru.rs b/components/salsa-2022/src/function/lru.rs index fdc80073..9d95f8b2 100644 --- a/components/salsa-2022/src/function/lru.rs +++ b/components/salsa-2022/src/function/lru.rs @@ -18,6 +18,7 @@ impl Lru { return None; } + let mut set = self.set.lock(); set.insert(index); if set.len() > capacity { diff --git a/salsa-2022-tests/tests/lru.rs b/salsa-2022-tests/tests/lru.rs index 324d1372..d6e3031d 100644 --- a/salsa-2022-tests/tests/lru.rs +++ b/salsa-2022-tests/tests/lru.rs @@ -9,7 +9,7 @@ use expect_test::expect; use test_log::test; #[salsa::jar(db = Db)] -struct Jar(MyInput, get_hot_potato); +struct Jar(MyInput, get_hot_potato, EmptyInput); trait Db: salsa::DbWithJar + HasLogger {} @@ -43,6 +43,9 @@ fn get_hot_potato(db: &dyn Db, input: MyInput) -> Arc { Arc::new(HotPotato::new(input.field(db))) } +#[salsa::input(jar = Jar)] +struct EmptyInput {} + #[salsa::db(Jar)] #[derive(Default)] struct Database { @@ -78,7 +81,6 @@ fn execute() { let p = get_hot_potato(&db, input); assert_eq!(p.0, i) } + EmptyInput::new(&mut db); assert_eq!(load_n_potatoes(), 32); - - } \ No newline at end of file