forked from salsa-rs/salsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccumulator.rs
162 lines (136 loc) · 3.97 KB
/
accumulator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Basic test of accumulator functionality.
use std::{
any::Any,
fmt::{self, Debug},
marker::PhantomData,
};
use accumulated::Accumulated;
use accumulated::AnyAccumulated;
use crate::{
cycle::CycleRecoveryStrategy,
ingredient::{fmt_index, Ingredient, Jar, MaybeChangedAfter},
plumbing::JarAux,
zalsa::IngredientIndex,
zalsa_local::QueryOrigin,
Database, DatabaseKeyIndex, Id, Revision,
};
mod accumulated;
pub(crate) mod accumulated_map;
/// Trait implemented on the struct that user annotated with `#[salsa::accumulator]`.
/// The `Self` type is therefore the types to be accumulated.
pub trait Accumulator: Clone + Debug + Send + Sync + Any + Sized {
const DEBUG_NAME: &'static str;
/// Accumulate an instance of this in the database for later retrieval.
fn accumulate<Db>(self, db: &Db)
where
Db: ?Sized + Database;
}
pub struct JarImpl<A: Accumulator> {
phantom: PhantomData<A>,
}
impl<A: Accumulator> Default for JarImpl<A> {
fn default() -> Self {
Self {
phantom: Default::default(),
}
}
}
impl<A: Accumulator> Jar for JarImpl<A> {
fn create_ingredients(
&self,
_aux: &dyn JarAux,
first_index: IngredientIndex,
) -> Vec<Box<dyn Ingredient>> {
vec![Box::new(<IngredientImpl<A>>::new(first_index))]
}
fn salsa_struct_type_id(&self) -> Option<std::any::TypeId> {
None
}
}
pub struct IngredientImpl<A: Accumulator> {
index: IngredientIndex,
phantom: PhantomData<Accumulated<A>>,
}
impl<A: Accumulator> IngredientImpl<A> {
/// Find the accumulator ingredient for `A` in the database, if any.
pub fn from_db<Db>(db: &Db) -> Option<&Self>
where
Db: ?Sized + Database,
{
let jar: JarImpl<A> = Default::default();
let zalsa = db.zalsa();
let index = zalsa.add_or_lookup_jar_by_type(&jar);
let ingredient = zalsa.lookup_ingredient(index).assert_type::<Self>();
Some(ingredient)
}
pub fn new(index: IngredientIndex) -> Self {
Self {
index,
phantom: PhantomData,
}
}
pub fn push(&self, db: &dyn Database, value: A) {
let zalsa_local = db.zalsa_local();
if let Err(()) = zalsa_local.accumulate(self.index, value) {
panic!("cannot accumulate values outside of an active tracked function");
}
}
pub fn index(&self) -> IngredientIndex {
self.index
}
}
impl<A: Accumulator> Ingredient for IngredientImpl<A> {
fn ingredient_index(&self) -> IngredientIndex {
self.index
}
fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Id,
_revision: Revision,
) -> MaybeChangedAfter {
panic!("nothing should ever depend on an accumulator directly")
}
fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy {
CycleRecoveryStrategy::Panic
}
fn origin(&self, _db: &dyn Database, _key_index: crate::Id) -> Option<QueryOrigin> {
None
}
fn mark_validated_output(
&self,
_db: &dyn Database,
_executor: DatabaseKeyIndex,
_output_key: crate::Id,
) {
}
fn remove_stale_output(
&self,
_db: &dyn Database,
_executor: DatabaseKeyIndex,
_stale_output_key: crate::Id,
) {
}
fn requires_reset_for_new_revision(&self) -> bool {
false
}
fn reset_for_new_revision(&mut self) {
panic!("unexpected reset on accumulator")
}
fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(A::DEBUG_NAME, index, fmt)
}
fn debug_name(&self) -> &'static str {
A::DEBUG_NAME
}
}
impl<A> std::fmt::Debug for IngredientImpl<A>
where
A: Accumulator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(std::any::type_name::<Self>())
.field("index", &self.index)
.finish()
}
}