Skip to content

Commit e28fe74

Browse files
committed
Refactor get_storage and set_storage
1 parent e95ad89 commit e28fe74

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

crates/building/src/base.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ pub struct Runtime {
4545
}
4646

4747
impl Runtime {
48-
pub fn compute<T>(&mut self, k: QueryKey, compute: impl Fn(&mut Runtime) -> T) -> T {
48+
pub fn compute<T: Clone>(
49+
&mut self,
50+
k: QueryKey,
51+
compute: impl Fn(&mut Runtime) -> T,
52+
set_storage: impl Fn(&mut Runtime, T),
53+
) -> T {
4954
let parent = mem::replace(&mut self.parent, Some(k));
50-
let result = compute(self);
55+
let value = compute(self);
5156

5257
self.revision += 1;
5358
let revision = self.revision;
@@ -63,14 +68,16 @@ impl Runtime {
6368
self.traces.insert(k, v);
6469

6570
self.parent = parent;
66-
result
71+
72+
set_storage(self, T::clone(&value));
73+
value
6774
}
6875

6976
pub fn query<T: Clone>(
7077
&mut self,
7178
k: QueryKey,
7279
compute: impl Fn(&mut Runtime) -> T,
73-
get_storage: impl Fn(&mut Runtime) -> Option<(T, &mut Trace)>,
80+
get_storage: impl Fn(&Runtime) -> Option<(T, &Trace)>,
7481
set_storage: impl Fn(&mut Runtime, T),
7582
) -> T {
7683
if let Some(parent) = self.parent {
@@ -105,15 +112,11 @@ impl Runtime {
105112
}
106113
value
107114
} else {
108-
let fresh = self.compute(k, compute);
109-
set_storage(self, T::clone(&fresh));
110-
fresh
115+
self.compute(k, compute, set_storage)
111116
}
112117
}
113118
} else {
114-
let fresh = self.compute(k, compute);
115-
set_storage(self, T::clone(&fresh));
116-
fresh
119+
self.compute(k, compute, set_storage)
117120
}
118121
}
119122

@@ -152,7 +155,7 @@ impl Runtime {
152155
},
153156
|this| {
154157
let value = this.parse.get(&id).cloned()?;
155-
let trace = this.traces.get_mut(&k)?;
158+
let trace = this.traces.get(&k)?;
156159
Some((value, trace))
157160
},
158161
|this, value| {
@@ -172,7 +175,7 @@ impl Runtime {
172175
},
173176
|this| {
174177
let value = this.index.get(&id).cloned()?;
175-
let trace = this.traces.get_mut(&k)?;
178+
let trace = this.traces.get(&k)?;
176179
Some((value, trace))
177180
},
178181
|this, value| {

0 commit comments

Comments
 (0)