mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-08-04 19:08:32 +00:00
Fix returns(deref | as_ref | as_deref) in tracked methods (#857)
This commit is contained in:
parent
13a2bd7461
commit
7edce6e248
3 changed files with 48 additions and 2 deletions
|
@ -110,7 +110,7 @@ macro_rules! setup_input_struct {
|
|||
|
||||
pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
|
||||
let zalsa_mut = db.zalsa_mut();
|
||||
let current_revision = zalsa_mut.new_revision();
|
||||
zalsa_mut.new_revision();
|
||||
let index = zalsa_mut.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
|
||||
let (ingredient, runtime) = zalsa_mut.lookup_ingredient_mut(index);
|
||||
let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();
|
||||
|
|
|
@ -298,7 +298,22 @@ impl Macro {
|
|||
) -> syn::Result<()> {
|
||||
if let Some(returns) = &args.returns {
|
||||
if let syn::ReturnType::Type(_, t) = &mut sig.output {
|
||||
**t = parse_quote!(& #db_lt #t)
|
||||
if returns == "copy" || returns == "clone" {
|
||||
// leave as is
|
||||
} else if returns == "ref" {
|
||||
**t = parse_quote!(& #db_lt #t)
|
||||
} else if returns == "deref" {
|
||||
**t = parse_quote!(& #db_lt <#t as ::core::ops::Deref>::Target)
|
||||
} else if returns == "as_ref" {
|
||||
**t = parse_quote!(<#t as ::salsa::SalsaAsRef>::AsRef<#db_lt>)
|
||||
} else if returns == "as_deref" {
|
||||
**t = parse_quote!(<#t as ::salsa::SalsaAsDeref>::AsDeref<#db_lt>)
|
||||
} else {
|
||||
return Err(syn::Error::new_spanned(
|
||||
returns,
|
||||
format!("Unknown returns mode `{returns}`"),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(syn::Error::new_spanned(
|
||||
returns,
|
||||
|
|
31
tests/tracked_method_inherent_return_deref.rs
Normal file
31
tests/tracked_method_inherent_return_deref.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use salsa::Database;
|
||||
|
||||
#[salsa::input]
|
||||
struct Input {
|
||||
number: usize,
|
||||
}
|
||||
|
||||
#[salsa::tracked]
|
||||
impl Input {
|
||||
#[salsa::tracked(returns(deref))]
|
||||
fn test(self, db: &dyn salsa::Database) -> Vec<String> {
|
||||
(0..self.number(db)).map(|i| format!("test {i}")).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invoke() {
|
||||
salsa::DatabaseImpl::new().attach(|db| {
|
||||
let input = Input::new(db, 3);
|
||||
let x: &[String] = input.test(db);
|
||||
|
||||
assert_eq!(
|
||||
x,
|
||||
&[
|
||||
"test 0".to_string(),
|
||||
"test 1".to_string(),
|
||||
"test 2".to_string()
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue