salsa/examples/hello_world/class_table.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2018-09-28 15:26:53 +00:00
use crate::compiler;
use salsa::{query_definition, query_prototype};
use std::sync::Arc;
pub trait ClassTableQueryContext: compiler::CompilerQueryContext {
query_prototype! {
2018-09-28 15:26:53 +00:00
/// Get the fields.
fn fields() for Fields;
2018-09-28 15:26:53 +00:00
/// Get the list of all classes
fn all_classes() for AllClasses;
2018-09-28 15:26:53 +00:00
/// Get the list of all fields
fn all_fields() for AllFields;
}
2018-09-28 15:26:53 +00:00
}
2018-09-28 15:40:20 +00:00
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct DefId(usize);
2018-09-28 15:26:53 +00:00
query_definition! {
pub AllClasses(_: &impl ClassTableQueryContext, (): ()) -> Arc<Vec<DefId>> {
2018-09-28 15:40:20 +00:00
Arc::new(vec![DefId(0), DefId(10)]) // dummy impl
2018-09-28 15:26:53 +00:00
}
}
query_definition! {
2018-09-28 15:40:20 +00:00
pub Fields(_: &impl ClassTableQueryContext, class: DefId) -> Arc<Vec<DefId>> {
Arc::new(vec![DefId(class.0 + 1), DefId(class.0 + 2)]) // dummy impl
2018-09-28 15:26:53 +00:00
}
}
query_definition! {
2018-09-28 15:40:20 +00:00
pub AllFields(query: &impl ClassTableQueryContext, (): ()) -> Arc<Vec<DefId>> {
2018-09-28 15:26:53 +00:00
Arc::new(
query.all_classes()
.of(())
.iter()
.cloned()
.flat_map(|def_id| {
let fields = query.fields().of(def_id);
2018-09-28 15:40:20 +00:00
(0..fields.len()).map(move |i| fields[i])
2018-09-28 15:26:53 +00:00
})
.collect()
)
}
}