mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-22 21:05:11 +00:00
add test for transparent queries
This commit is contained in:
parent
1b873281a1
commit
7dcbaf4f2f
6 changed files with 115 additions and 21 deletions
|
@ -4,7 +4,6 @@ use salsa::dyn_descriptor::DynDescriptor;
|
|||
use salsa::query_context_storage;
|
||||
use salsa::BaseQueryContext;
|
||||
use salsa::Query;
|
||||
use salsa::QueryTable;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::Write;
|
||||
|
||||
|
|
45
examples/storage_varieties/implementation.rs
Normal file
45
examples/storage_varieties/implementation.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use crate::queries;
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct QueryContextImpl {
|
||||
storage: QueryContextImplStorage,
|
||||
counter: Cell<usize>,
|
||||
}
|
||||
|
||||
salsa::query_context_storage! {
|
||||
struct QueryContextImplStorage for storage in QueryContextImpl {
|
||||
impl queries::QueryContext {
|
||||
fn memoized() for queries::Memoized;
|
||||
fn transparent() for queries::Transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl queries::CounterContext for QueryContextImpl {
|
||||
fn increment(&self) -> usize {
|
||||
let v = self.counter.get();
|
||||
self.counter.set(v + 1);
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
impl salsa::BaseQueryContext for QueryContextImpl {
|
||||
type QueryDescriptor = salsa::dyn_descriptor::DynDescriptor;
|
||||
|
||||
fn execute_query_implementation<Q>(
|
||||
&self,
|
||||
_descriptor: Self::QueryDescriptor,
|
||||
key: &Q::Key,
|
||||
) -> Q::Value
|
||||
where
|
||||
Q: salsa::Query<Self>,
|
||||
{
|
||||
let value = Q::execute(self, key.clone());
|
||||
value
|
||||
}
|
||||
|
||||
fn report_unexpected_cycle(&self, _descriptor: Self::QueryDescriptor) -> ! {
|
||||
panic!("cycle")
|
||||
}
|
||||
}
|
5
examples/storage_varieties/main.rs
Normal file
5
examples/storage_varieties/main.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
#![feature(crate_visibility_modifier)]
|
||||
|
||||
mod implementation;
|
||||
mod queries;
|
||||
mod tests;
|
23
examples/storage_varieties/queries.rs
Normal file
23
examples/storage_varieties/queries.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
crate trait CounterContext: salsa::BaseQueryContext {
|
||||
fn increment(&self) -> usize;
|
||||
}
|
||||
|
||||
crate trait QueryContext: CounterContext {
|
||||
salsa::query_prototype! {
|
||||
fn memoized() for Memoized;
|
||||
fn transparent() for Transparent;
|
||||
}
|
||||
}
|
||||
|
||||
salsa::query_definition! {
|
||||
crate Memoized(query: &impl QueryContext, (): ()) -> usize {
|
||||
query.increment()
|
||||
}
|
||||
}
|
||||
|
||||
salsa::query_definition! {
|
||||
#[storage(transparent)]
|
||||
crate Transparent(query: &impl QueryContext, (): ()) -> usize {
|
||||
query.increment()
|
||||
}
|
||||
}
|
18
examples/storage_varieties/tests.rs
Normal file
18
examples/storage_varieties/tests.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use crate::implementation::QueryContextImpl;
|
||||
use crate::queries::QueryContext;
|
||||
|
||||
#[test]
|
||||
fn memoized_twice() {
|
||||
let query = QueryContextImpl::default();
|
||||
let v1 = query.memoized().of(());
|
||||
let v2 = query.memoized().of(());
|
||||
assert_eq!(v1, v2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparent_twice() {
|
||||
let query = QueryContextImpl::default();
|
||||
let v1 = query.transparent().of(());
|
||||
let v2 = query.transparent().of(());
|
||||
assert_eq!(v1 + 1, v2);
|
||||
}
|
44
src/lib.rs
44
src/lib.rs
|
@ -186,64 +186,55 @@ macro_rules! query_definition {
|
|||
// we consume.
|
||||
(
|
||||
@filter_attrs {
|
||||
input { #[storage(memoized)] $(#[$attr:meta])* };
|
||||
input { #[storage(memoized)] $($input:tt)* };
|
||||
storage { $storage:tt };
|
||||
other_attrs { $($other_attrs:tt)* };
|
||||
tokens { $($tokens:tt)* };
|
||||
}
|
||||
) => {
|
||||
$crate::query_definition! {
|
||||
@filter_attrs {
|
||||
input { $(#[$attr])* };
|
||||
input { $($input)* };
|
||||
storage { memoized };
|
||||
other_attrs { $($other_attrs)* };
|
||||
tokens { $($tokens)* };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
@filter_attrs {
|
||||
input { #[storage(transparent)] $(#[$attr:meta])* };
|
||||
input { #[storage(transparent)] $($input:tt)* };
|
||||
storage { $storage:tt };
|
||||
other_attrs { $($other_attrs:tt)* };
|
||||
tokens { $($tokens:tt)* };
|
||||
}
|
||||
) => {
|
||||
$crate::query_definition! {
|
||||
@filter_attrs {
|
||||
input { $(#[$attr])* };
|
||||
input { $($input)* };
|
||||
storage { transparent };
|
||||
other_attrs { $($other_attrs)* };
|
||||
tokens { $($tokens)* };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
@filter_attrs {
|
||||
input { #[$attr:meta] $(#[$attrs:meta])* };
|
||||
input { #[$attr:meta] $($input:tt)* };
|
||||
storage { $storage:tt };
|
||||
other_attrs { $($other_attrs:tt)* };
|
||||
tokens { $($tokens:tt)* };
|
||||
}
|
||||
) => {
|
||||
$crate::query_definition! {
|
||||
@filter_attrs {
|
||||
input { $(#[$attrs])* };
|
||||
input { $($input)* };
|
||||
storage { $storage };
|
||||
other_attrs { $($other_attrs)* #[$attr] };
|
||||
tokens { $($tokens)* };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
@filter_attrs {
|
||||
input { };
|
||||
storage { $storage:tt };
|
||||
other_attrs { $($attrs:tt)* };
|
||||
tokens {
|
||||
input {
|
||||
$v:vis $name:ident(
|
||||
$query:tt : &impl $query_trait:path,
|
||||
$key:tt : $key_ty:ty $(,)*
|
||||
|
@ -251,6 +242,8 @@ macro_rules! query_definition {
|
|||
$($body:tt)*
|
||||
}
|
||||
};
|
||||
storage { $storage:tt };
|
||||
other_attrs { $($attrs:tt)* };
|
||||
}
|
||||
) => {
|
||||
#[derive(Default, Debug)]
|
||||
|
@ -283,15 +276,26 @@ macro_rules! query_definition {
|
|||
$crate::transparent::TransparentStorage
|
||||
};
|
||||
|
||||
// Various legal start states:
|
||||
(
|
||||
$(#[$attr:meta])* $($tokens:tt)*
|
||||
# $($tokens:tt)*
|
||||
) => {
|
||||
$crate::query_definition! {
|
||||
@filter_attrs {
|
||||
input { $(#[$attr])* };
|
||||
input { # $($tokens)* };
|
||||
storage { memoized };
|
||||
other_attrs { };
|
||||
}
|
||||
}
|
||||
};
|
||||
(
|
||||
$v:vis $name:ident $($tokens:tt)*
|
||||
) => {
|
||||
$crate::query_definition! {
|
||||
@filter_attrs {
|
||||
input { $v $name $($tokens)* };
|
||||
storage { memoized };
|
||||
other_attrs { };
|
||||
tokens { $($tokens)* };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -328,7 +332,7 @@ macro_rules! query_context_storage {
|
|||
fn $query_method(
|
||||
&self,
|
||||
) -> $crate::QueryTable<'_, Self, $QueryType> {
|
||||
QueryTable::new(
|
||||
$crate::QueryTable::new(
|
||||
self,
|
||||
&self.$storage_field.$query_method,
|
||||
|
||||
|
|
Loading…
Reference in a new issue