Skip to content

Commit 0d9fa8b

Browse files
committed
Introduce ChalkDatabase::with_program helper
1 parent fcb9434 commit 0d9fa8b

File tree

4 files changed

+49
-50
lines changed

4 files changed

+49
-50
lines changed

chalk-solve/src/solve/test/bench.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@ fn run_bench(
1919
bencher: &mut Bencher,
2020
expected: &str,
2121
) {
22-
let mut db = ChalkDatabase::default();
23-
24-
db.query_mut(ProgramText)
25-
.set((), Arc::new(text.to_string()));
26-
db.query_mut(ProgramSolverChoice).set((), solver_choice);
27-
28-
let program = db.lowered_program().unwrap();
29-
let env = db.environment().unwrap();
30-
ir::tls::set_current_program(&program, || {
31-
let goal = parse_and_lower_goal(&program, goal_text).unwrap();
32-
let peeled_goal = goal.into_peeled_goal();
33-
34-
// Execute once to get an expected result.
35-
let result = solver_choice.solve_root_goal(&env, &peeled_goal);
36-
37-
// Check expectation.
38-
assert_result(&result, expected);
39-
40-
// Then do it many times to measure time.
41-
bencher.iter(|| solver_choice.solve_root_goal(&env, &peeled_goal));
22+
ChalkDatabase::with_program(Arc::new(program_text.to_string()), solver_choice, |db| {
23+
let program = db.lowered_program().unwrap();
24+
let env = db.environment().unwrap();
25+
ir::tls::set_current_program(&program, || {
26+
let goal = parse_and_lower_goal(&program, goal_text).unwrap();
27+
let peeled_goal = goal.into_peeled_goal();
28+
29+
// Execute once to get an expected result.
30+
let result = solver_choice.solve_root_goal(&env, &peeled_goal);
31+
32+
// Check expectation.
33+
assert_result(&result, expected);
34+
35+
// Then do it many times to measure time.
36+
bencher.iter(|| solver_choice.solve_root_goal(&env, &peeled_goal));
37+
});
4238
});
4339
}
4440

src/bin/chalki.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ use std::process::exit;
1818
use std::sync::Arc;
1919

2020
use chalk::db::ChalkDatabase;
21-
use chalk::query::{LoweringDatabase, ProgramSolverChoice, ProgramText};
21+
use chalk::query::LoweringDatabase;
2222
use chalk::rust_ir;
2323
use chalk::rust_ir::lowering::*;
2424
use chalk_engine::fallible::NoSolution;
2525
use chalk_solve::ext::*;
2626
use chalk_solve::solve::SolverChoice;
2727
use docopt::Docopt;
2828
use rustyline::error::ReadlineError;
29-
use salsa::Database;
3029

3130
const USAGE: &'static str = "
3231
chalk repl
@@ -77,15 +76,11 @@ impl Program {
7776
///
7877
/// [`SolverChoice`]: struct.solve.SolverChoice.html
7978
fn new(text: String, solver_choice: SolverChoice) -> Result<Program> {
80-
let mut db = ChalkDatabase::default();
81-
82-
db.query_mut(ProgramText)
83-
.set((), Arc::new(text.to_string()));
84-
db.query_mut(ProgramSolverChoice).set((), solver_choice);
85-
86-
let ir = db.checked_program().unwrap();
87-
let env = db.environment().unwrap();
88-
Ok(Program { text, ir, env })
79+
ChalkDatabase::with_program(Arc::new(text.clone()), solver_choice, |db| {
80+
let ir = db.checked_program().unwrap();
81+
let env = db.environment().unwrap();
82+
Ok(Program { text, ir, env })
83+
})
8984
}
9085
}
9186

src/db.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
use crate::query;
1+
use crate::query::{self, ProgramSolverChoice, ProgramText};
2+
use chalk_solve::solve::SolverChoice;
3+
use salsa::Database;
4+
use std::sync::Arc;
25

36
#[derive(Default)]
47
pub struct ChalkDatabase {
58
runtime: salsa::Runtime<ChalkDatabase>,
69
}
710

8-
impl salsa::Database for ChalkDatabase {
11+
impl Database for ChalkDatabase {
912
fn salsa_runtime(&self) -> &salsa::Runtime<ChalkDatabase> {
1013
&self.runtime
1114
}
1215
}
1316

17+
impl ChalkDatabase {
18+
pub fn with_program<F: FnOnce(&mut ChalkDatabase) -> R, R>(
19+
program_text: Arc<String>,
20+
solver_choice: SolverChoice,
21+
f: F,
22+
) -> R {
23+
let mut db = ChalkDatabase::default();
24+
25+
db.query_mut(ProgramText).set((), program_text);
26+
db.query_mut(ProgramSolverChoice).set((), solver_choice);
27+
28+
f(&mut db)
29+
}
30+
}
31+
1432
salsa::database_storage! {
1533
pub struct DatabaseStorage for ChalkDatabase {
1634
impl query::LoweringDatabase {

src/test_util.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::db::ChalkDatabase;
44
use crate::errors::Result;
55
use crate::query::LoweringDatabase;
6-
use crate::query::{ProgramSolverChoice, ProgramText};
76
use crate::rust_ir::lowering::LowerGoal;
87
use crate::rust_ir::Program;
98
use chalk_ir::Goal;
@@ -12,7 +11,6 @@ use chalk_parse;
1211
use chalk_solve::solve::SolverChoice;
1312
use diff;
1413
use itertools::Itertools;
15-
use salsa::Database;
1614
use std::fmt::Write;
1715
use std::prelude::v1::Result as StdResult;
1816
use std::sync::Arc;
@@ -21,27 +19,19 @@ pub fn parse_and_lower_program(
2119
text: &str,
2220
solver_choice: SolverChoice,
2321
) -> StdResult<Arc<Program>, String> {
24-
let mut db = ChalkDatabase::default();
25-
26-
db.query_mut(ProgramText)
27-
.set((), Arc::new(text.to_string()));
28-
db.query_mut(ProgramSolverChoice).set((), solver_choice);
29-
30-
db.checked_program()
22+
ChalkDatabase::with_program(Arc::new(text.to_string()), solver_choice, |db| {
23+
db.checked_program()
24+
})
3125
}
3226

3327
pub fn parse_and_lower_program_with_env(
3428
text: &str,
3529
solver_choice: SolverChoice,
3630
) -> StdResult<(Arc<Program>, Arc<ProgramEnvironment>), String> {
37-
let mut db = ChalkDatabase::default();
38-
39-
db.query_mut(ProgramText)
40-
.set((), Arc::new(text.to_string()));
41-
db.query_mut(ProgramSolverChoice).set((), solver_choice);
42-
43-
db.checked_program()
44-
.and_then(|program| Ok((program, db.environment()?)))
31+
ChalkDatabase::with_program(Arc::new(text.to_string()), solver_choice, |db| {
32+
db.checked_program()
33+
.and_then(|program| Ok((program, db.environment()?)))
34+
})
4535
}
4636

4737
pub fn parse_and_lower_goal(program: &Program, text: &str) -> Result<Box<Goal>> {

0 commit comments

Comments
 (0)