mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-08-04 19:08:32 +00:00
switch to use fn
This commit is contained in:
parent
1b98ecb7a8
commit
55ec1f51d3
8 changed files with 129 additions and 87 deletions
|
@ -24,28 +24,22 @@ query_prototype! {
|
|||
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct DefId(usize);
|
||||
|
||||
impl<DB: ClassTableDatabase> salsa::QueryFunction<DB> for AllClasses {
|
||||
fn execute(_: &DB, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![DefId(0), DefId(10)]) // dummy impl
|
||||
}
|
||||
fn all_classes(_: &impl ClassTableDatabase, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![DefId(0), DefId(10)]) // dummy impl
|
||||
}
|
||||
|
||||
impl<DB: ClassTableDatabase> salsa::QueryFunction<DB> for Fields {
|
||||
fn execute(_: &DB, class: DefId) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![DefId(class.0 + 1), DefId(class.0 + 2)]) // dummy impl
|
||||
}
|
||||
fn fields(_: &impl ClassTableDatabase, class: DefId) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![DefId(class.0 + 1), DefId(class.0 + 2)]) // dummy impl
|
||||
}
|
||||
|
||||
impl<DB: ClassTableDatabase> salsa::QueryFunction<DB> for AllFields {
|
||||
fn execute(db: &DB, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(
|
||||
db.all_classes(())
|
||||
.iter()
|
||||
.cloned()
|
||||
.flat_map(|def_id| {
|
||||
let fields = db.fields(def_id);
|
||||
(0..fields.len()).map(move |i| fields[i])
|
||||
}).collect(),
|
||||
)
|
||||
}
|
||||
fn all_fields(db: &impl ClassTableDatabase, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(
|
||||
db.all_classes(())
|
||||
.iter()
|
||||
.cloned()
|
||||
.flat_map(|def_id| {
|
||||
let fields = db.fields(def_id);
|
||||
(0..fields.len()).map(move |i| fields[i])
|
||||
}).collect(),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -31,14 +31,12 @@ salsa::query_prototype! {
|
|||
// HelloWorldDatabase`) that gives access to other queries. The runtime
|
||||
// will track which queries you use so that we can incrementally
|
||||
// update memoized results.
|
||||
impl<DB: HelloWorldDatabase> salsa::QueryFunction<DB> for Length {
|
||||
fn execute(db: &DB, _key: ()) -> usize {
|
||||
// Read the input string:
|
||||
let input_string = db.input_string(());
|
||||
fn length(db: &impl HelloWorldDatabase, (): ()) -> usize {
|
||||
// Read the input string:
|
||||
let input_string = db.input_string(());
|
||||
|
||||
// Return its length:
|
||||
input_string.len()
|
||||
}
|
||||
// Return its length:
|
||||
input_string.len()
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
|
76
src/lib.rs
76
src/lib.rs
|
@ -224,6 +224,7 @@ impl DefaultKey for () {
|
|||
/// fn my_query(input: u32) -> u64 {
|
||||
/// type MyQuery;
|
||||
/// storage memoized; // optional, this is the default
|
||||
/// use fn path::to::fn; // optional, default is `my_query`
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
|
@ -260,6 +261,7 @@ macro_rules! query_prototype {
|
|||
fn $method_name:ident($key_name:ident: $key_ty:ty) -> $value_ty:ty {
|
||||
type $QueryType:ident;
|
||||
$(storage $storage:ident;)* // FIXME(rust-lang/rust#48075) should be `?`
|
||||
$(use fn $fn_path:path;)* // FIXME(rust-lang/rust#48075) should be `?`
|
||||
}
|
||||
)*
|
||||
}];
|
||||
|
@ -286,9 +288,83 @@ macro_rules! query_prototype {
|
|||
type Value = $value_ty;
|
||||
type Storage = $crate::query_prototype! { @storage_ty[DB, Self, $($storage)*] };
|
||||
}
|
||||
|
||||
$crate::query_prototype! {
|
||||
@query_fn[
|
||||
storage($($storage)*);
|
||||
method_name($method_name);
|
||||
fn_path($($fn_path)*);
|
||||
db_trait($query_trait);
|
||||
query_type($QueryType);
|
||||
]
|
||||
}
|
||||
)*
|
||||
};
|
||||
|
||||
(
|
||||
@query_fn[
|
||||
storage(input);
|
||||
method_name($method_name:ident);
|
||||
fn_path();
|
||||
$($rest:tt)*
|
||||
]
|
||||
) => {
|
||||
// do nothing for `storage input`, presuming they did not write an explicit `use fn`
|
||||
};
|
||||
|
||||
(
|
||||
@query_fn[
|
||||
storage(input);
|
||||
method_name($method_name:ident);
|
||||
fn_path($($fn_path:tt)+);
|
||||
$($rest:tt)*
|
||||
]
|
||||
) => {
|
||||
// error for `storage input` with an explicit `use fn`
|
||||
compile_error! {
|
||||
"cannot have `storage input` combined with `use fn`"
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
@query_fn[
|
||||
storage($($storage:ident)*);
|
||||
method_name($method_name:ident);
|
||||
fn_path();
|
||||
$($rest:tt)*
|
||||
]
|
||||
) => {
|
||||
// default to `use fn $method_name`
|
||||
$crate::query_prototype! {
|
||||
@query_fn[
|
||||
storage($($storage)*);
|
||||
method_name($method_name);
|
||||
fn_path($method_name);
|
||||
$($rest)*
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
@query_fn[
|
||||
storage($($storage:ident)*);
|
||||
method_name($method_name:ident);
|
||||
fn_path($fn_path:path);
|
||||
db_trait($DbTrait:path);
|
||||
query_type($QueryType:ty);
|
||||
]
|
||||
) => {
|
||||
impl<DB> $crate::QueryFunction<DB> for $QueryType
|
||||
where DB: $DbTrait
|
||||
{
|
||||
fn execute(db: &DB, key: <Self as $crate::Query<DB>>::Key)
|
||||
-> <Self as $crate::Query<DB>>::Value
|
||||
{
|
||||
$fn_path(db, key)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Recursive case: found some more part of the trait header.
|
||||
// Keep pulling out tokens until we find the body.
|
||||
(
|
||||
|
|
|
@ -42,28 +42,20 @@ salsa::query_prototype! {
|
|||
}
|
||||
}
|
||||
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for MemoizedA {
|
||||
fn execute(db: &DB, (): ()) -> () {
|
||||
db.memoized_b(())
|
||||
}
|
||||
fn memoized_a(db: &impl Database, (): ()) -> () {
|
||||
db.memoized_b(())
|
||||
}
|
||||
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for MemoizedB {
|
||||
fn execute(db: &DB, (): ()) -> () {
|
||||
db.memoized_a(())
|
||||
}
|
||||
fn memoized_b(db: &impl Database, (): ()) -> () {
|
||||
db.memoized_a(())
|
||||
}
|
||||
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for VolatileA {
|
||||
fn execute(db: &DB, (): ()) -> () {
|
||||
db.volatile_b(())
|
||||
}
|
||||
fn volatile_a(db: &impl Database, (): ()) -> () {
|
||||
db.volatile_b(())
|
||||
}
|
||||
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for VolatileB {
|
||||
fn execute(db: &DB, (): ()) -> () {
|
||||
db.volatile_a(())
|
||||
}
|
||||
fn volatile_b(db: &impl Database, (): ()) -> () {
|
||||
db.volatile_a(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -24,25 +24,19 @@ salsa::query_prototype! {
|
|||
}
|
||||
}
|
||||
|
||||
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Memoized2 {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Memoized2 invoked");
|
||||
db.dep_memoized1(())
|
||||
}
|
||||
fn dep_memoized2(db: &impl MemoizedDepInputsContext, (): ()) -> usize {
|
||||
db.log().add("Memoized2 invoked");
|
||||
db.dep_memoized1(())
|
||||
}
|
||||
|
||||
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Memoized1 {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Memoized1 invoked");
|
||||
db.dep_derived1(()) * 2
|
||||
}
|
||||
fn dep_memoized1(db: &impl MemoizedDepInputsContext, (): ()) -> usize {
|
||||
db.log().add("Memoized1 invoked");
|
||||
db.dep_derived1(()) * 2
|
||||
}
|
||||
|
||||
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Derived1 {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Derived1 invoked");
|
||||
db.dep_input1(()) / 2
|
||||
}
|
||||
fn dep_derived1(db: &impl MemoizedDepInputsContext, (): ()) -> usize {
|
||||
db.log().add("Derived1 invoked");
|
||||
db.dep_input1(()) / 2
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -17,11 +17,9 @@ salsa::query_prototype! {
|
|||
}
|
||||
}
|
||||
|
||||
impl<DB: MemoizedInputsContext> salsa::QueryFunction<DB> for Max {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Max invoked");
|
||||
std::cmp::max(db.input1(()), db.input2(()))
|
||||
}
|
||||
fn max(db: &impl MemoizedInputsContext, (): ()) -> usize {
|
||||
db.log().add("Max invoked");
|
||||
std::cmp::max(db.input1(()), db.input2(()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -18,26 +18,20 @@ salsa::query_prototype! {
|
|||
}
|
||||
}
|
||||
|
||||
impl<DB: MemoizedVolatileContext> salsa::QueryFunction<DB> for Memoized2 {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Memoized2 invoked");
|
||||
db.memoized1(())
|
||||
}
|
||||
fn memoized2(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||
db.log().add("Memoized2 invoked");
|
||||
db.memoized1(())
|
||||
}
|
||||
|
||||
impl<DB: MemoizedVolatileContext> salsa::QueryFunction<DB> for Memoized1 {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Memoized1 invoked");
|
||||
let v = db.volatile(());
|
||||
v / 2
|
||||
}
|
||||
fn memoized1(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||
db.log().add("Memoized1 invoked");
|
||||
let v = db.volatile(());
|
||||
v / 2
|
||||
}
|
||||
|
||||
impl<DB: MemoizedVolatileContext> salsa::QueryFunction<DB> for Volatile {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.log().add("Volatile invoked");
|
||||
db.clock().increment()
|
||||
}
|
||||
fn volatile(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||
db.log().add("Volatile invoked");
|
||||
db.clock().increment()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -16,16 +16,12 @@ salsa::query_prototype! {
|
|||
|
||||
/// Because this query is memoized, we only increment the counter
|
||||
/// the first time it is invoked.
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for Memoized {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.increment()
|
||||
}
|
||||
fn memoized(db: &impl Database, (): ()) -> usize {
|
||||
db.increment()
|
||||
}
|
||||
|
||||
/// Because this query is volatile, each time it is invoked,
|
||||
/// we will increment the counter.
|
||||
impl<DB: Database> salsa::QueryFunction<DB> for Volatile {
|
||||
fn execute(db: &DB, (): ()) -> usize {
|
||||
db.increment()
|
||||
}
|
||||
fn volatile(db: &impl Database, (): ()) -> usize {
|
||||
db.increment()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue