mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-22 21:05:11 +00:00
make the example build
This commit is contained in:
parent
dcc14476f9
commit
3646890b38
6 changed files with 121 additions and 10 deletions
|
@ -19,23 +19,23 @@ pub trait ClassTableQueryContext: compiler::CompilerQueryContext {
|
|||
);
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct DefId;
|
||||
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct DefId(usize);
|
||||
|
||||
query_definition! {
|
||||
pub AllClasses(_: &impl ClassTableQueryContext, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![]) // dummy impl
|
||||
Arc::new(vec![DefId(0), DefId(10)]) // dummy impl
|
||||
}
|
||||
}
|
||||
|
||||
query_definition! {
|
||||
pub Fields(_: &impl ClassTableQueryContext, _class: DefId) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![]) // dummy impl
|
||||
pub Fields(_: &impl ClassTableQueryContext, class: DefId) -> Arc<Vec<DefId>> {
|
||||
Arc::new(vec![DefId(class.0 + 1), DefId(class.0 + 2)]) // dummy impl
|
||||
}
|
||||
}
|
||||
|
||||
query_definition! {
|
||||
pub AllFields(query: &impl ClassTableQueryContext, _class: DefId) -> Arc<Vec<DefId>> {
|
||||
pub AllFields(query: &impl ClassTableQueryContext, (): ()) -> Arc<Vec<DefId>> {
|
||||
Arc::new(
|
||||
query.all_classes()
|
||||
.of(())
|
||||
|
@ -43,7 +43,7 @@ query_definition! {
|
|||
.cloned()
|
||||
.flat_map(|def_id| {
|
||||
let fields = query.fields().of(def_id);
|
||||
(0..fields.len()).map(move |i| fields[i].clone())
|
||||
(0..fields.len()).map(move |i| fields[i])
|
||||
})
|
||||
.collect()
|
||||
)
|
||||
|
|
|
@ -2,5 +2,5 @@ pub trait CompilerQueryContext: salsa::BaseQueryContext {
|
|||
fn interner(&self) -> &Interner;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Interner;
|
||||
|
|
92
examples/hello_world/implementation.rs
Normal file
92
examples/hello_world/implementation.rs
Normal file
|
@ -0,0 +1,92 @@
|
|||
use crate::class_table::{self, ClassTableQueryContext};
|
||||
use crate::compiler::{CompilerQueryContext, Interner};
|
||||
use salsa::dyn_descriptor::DynDescriptor;
|
||||
use salsa::BaseQueryContext;
|
||||
use salsa::Query;
|
||||
use salsa::QueryTable;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct QueryContextImpl {
|
||||
storage: QueryContextImplStorage,
|
||||
interner: Interner,
|
||||
execution_stack: RefCell<Vec<DynDescriptor>>,
|
||||
}
|
||||
|
||||
// The intention is that plus the impl of `ClassTableQueryContext`
|
||||
// below will eventually be generated by a macro, so that you just
|
||||
// have to name the queries.
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Default)]
|
||||
struct QueryContextImplStorage {
|
||||
AllClasses: <class_table::AllClasses as Query<QueryContextImpl>>::Storage,
|
||||
AllFields: <class_table::AllFields as Query<QueryContextImpl>>::Storage,
|
||||
Fields: <class_table::Fields as Query<QueryContextImpl>>::Storage,
|
||||
}
|
||||
|
||||
impl ClassTableQueryContext for QueryContextImpl {
|
||||
fn all_classes(&self) -> QueryTable<'_, Self, class_table::AllClasses> {
|
||||
QueryTable::new(
|
||||
self,
|
||||
&self.storage.AllClasses,
|
||||
DynDescriptor::from_key::<Self, class_table::AllClasses>,
|
||||
)
|
||||
}
|
||||
|
||||
fn all_fields(&self) -> QueryTable<'_, Self, class_table::AllFields> {
|
||||
QueryTable::new(
|
||||
self,
|
||||
&self.storage.AllFields,
|
||||
DynDescriptor::from_key::<Self, class_table::AllFields>,
|
||||
)
|
||||
}
|
||||
|
||||
fn fields(&self) -> QueryTable<'_, Self, class_table::Fields> {
|
||||
QueryTable::new(
|
||||
self,
|
||||
&self.storage.Fields,
|
||||
DynDescriptor::from_key::<Self, class_table::Fields>,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl CompilerQueryContext for QueryContextImpl {
|
||||
fn interner(&self) -> &Interner {
|
||||
&self.interner
|
||||
}
|
||||
}
|
||||
|
||||
impl BaseQueryContext for QueryContextImpl {
|
||||
type QueryDescriptor = DynDescriptor;
|
||||
|
||||
fn execute_query_implementation<Q>(
|
||||
&self,
|
||||
descriptor: Self::QueryDescriptor,
|
||||
key: &Q::Key,
|
||||
) -> Q::Value
|
||||
where
|
||||
Q: Query<Self>,
|
||||
{
|
||||
self.execution_stack.borrow_mut().push(descriptor);
|
||||
let value = Q::execute(self, key.clone());
|
||||
self.execution_stack.borrow_mut().pop();
|
||||
value
|
||||
}
|
||||
|
||||
fn report_unexpected_cycle(&self, descriptor: Self::QueryDescriptor) -> ! {
|
||||
let execution_stack = self.execution_stack.borrow();
|
||||
let start_index = (0..execution_stack.len())
|
||||
.rev()
|
||||
.filter(|&i| execution_stack[i] == descriptor)
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
let mut message = format!("Internal error, cycle detected:\n");
|
||||
for descriptor in &execution_stack[start_index..] {
|
||||
writeln!(message, "- {:?}\n", descriptor).unwrap();
|
||||
}
|
||||
panic!(message)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,13 @@
|
|||
mod class_table;
|
||||
mod compiler;
|
||||
mod implementation;
|
||||
|
||||
use self::class_table::ClassTableQueryContext;
|
||||
use self::implementation::QueryContextImpl;
|
||||
|
||||
fn main() {
|
||||
println!("It builds.");
|
||||
let query = QueryContextImpl::default();
|
||||
for f in query.all_fields().of(()).iter() {
|
||||
println!("{:?}", f);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ pub trait Query<QC: BaseQueryContext>: Debug + Default + Sized + 'static {
|
|||
fn execute(query: &QC, key: Self::Key) -> Self::Value;
|
||||
}
|
||||
|
||||
pub trait QueryStorageOps<QC, Q>
|
||||
pub trait QueryStorageOps<QC, Q>: Default
|
||||
where
|
||||
QC: BaseQueryContext,
|
||||
Q: Query<QC>,
|
||||
|
|
|
@ -25,6 +25,18 @@ where
|
|||
map: RefCell<FxHashMap<Q::Key, QueryState<Q::Value>>>,
|
||||
}
|
||||
|
||||
impl<QC, Q> Default for MemoizedStorage<QC, Q>
|
||||
where
|
||||
Q: Query<QC>,
|
||||
QC: BaseQueryContext,
|
||||
{
|
||||
fn default() -> Self {
|
||||
MemoizedStorage {
|
||||
map: RefCell::new(FxHashMap::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<QC, Q> QueryStorageOps<QC, Q> for MemoizedStorage<QC, Q>
|
||||
where
|
||||
Q: Query<QC>,
|
||||
|
|
Loading…
Reference in a new issue