make lru option work

This commit is contained in:
XFFXFF 2022-08-16 08:46:59 +08:00
parent 2d59df39af
commit d080e349ef
3 changed files with 49 additions and 19 deletions

View file

@ -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 = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::ingredient(jar);
&ingredients.function
},
|jars| {
let jar = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars_mut(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::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 = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::ingredient(jar);
&ingredients.function
},
|jars| {
let jar = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars_mut(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::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 = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::ingredient(jar);
&ingredients.function
},
|jars| {
let jar = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars_mut(jars);
let ingredients =
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::ingredient_mut(jar);
&mut ingredients.function
});
salsa::function::FunctionIngredient::new(index)
},
function: #function
}
}
}

View file

@ -18,6 +18,7 @@ impl Lru {
return None;
}
let mut set = self.set.lock();
set.insert(index);
if set.len() > capacity {

View file

@ -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<Jar> + HasLogger {}
@ -43,6 +43,9 @@ fn get_hot_potato(db: &dyn Db, input: MyInput) -> Arc<HotPotato> {
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);
}