Skip to content

Commit fa8ef9a

Browse files
committed
---
yaml --- r: 67223 b: refs/heads/master c: 31c180e h: refs/heads/master i: 67221: aa232e9 67219: eef6dfb 67215: 40b7775 v: v3
1 parent 69c2b71 commit fa8ef9a

File tree

2 files changed

+72
-59
lines changed

2 files changed

+72
-59
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 1d9181bd760cb89171e892204338bb64275eb4e5
2+
refs/heads/master: 31c180e5f5da0e27c60a3bf02da182f19c66cf8f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/src/libextra/workcache.rs

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ struct Database {
124124
}
125125

126126
impl Database {
127+
128+
pub fn new(p: Path) -> Database {
129+
Database {
130+
db_filename: p,
131+
db_cache: TreeMap::new(),
132+
db_dirty: false
133+
}
134+
}
135+
127136
pub fn prepare(&self,
128137
fn_name: &str,
129138
declared_inputs: &WorkMap)
@@ -156,22 +165,26 @@ struct Logger {
156165
}
157166

158167
impl Logger {
168+
169+
pub fn new() -> Logger {
170+
Logger { a: () }
171+
}
172+
159173
pub fn info(&self, i: &str) {
160174
io::println(~"workcache: " + i);
161175
}
162176
}
163177
164178
struct Context {
165179
db: RWARC<Database>,
166-
logger: @mut Logger,
180+
logger: Logger,
167181
cfg: json::Object,
168182
freshness: TreeMap<~str,@fn(&str,&str)->bool>
169183
}
170184
171-
#[deriving(Clone)]
172-
struct Prep {
173-
ctxt: @Context,
174-
fn_name: ~str,
185+
struct Prep<'self> {
186+
ctxt: &'self Context,
187+
fn_name: &'self str,
175188
declared_inputs: WorkMap,
176189
}
177190
@@ -180,8 +193,8 @@ struct Exec {
180193
discovered_outputs: WorkMap
181194
}
182195
183-
struct Work<T> {
184-
prep: @mut Prep,
196+
struct Work<'self, T> {
197+
prep: &'self Prep<'self>,
185198
res: Option<Either<T,PortOne<(Exec,T)>>>
186199
}
187200
@@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str {
215228
}
216229
217230
impl Context {
218-
pub fn new(db: RWARC<Database>, lg: @mut Logger, cfg: json::Object)
219-
-> Context {
231+
232+
pub fn new(db: RWARC<Database>, lg: Logger, cfg: json::Object) -> Context {
220233
Context {
221234
db: db,
222235
logger: lg,
@@ -225,33 +238,28 @@ impl Context {
225238
}
226239
}
227240
228-
pub fn prep<T:Send +
229-
Encodable<json::Encoder> +
230-
Decodable<json::Decoder>>(@self, // FIXME(#5121)
231-
fn_name:&str,
232-
blk: &fn(@mut Prep)->Work<T>)
233-
-> Work<T> {
234-
let p = @mut Prep {
235-
ctxt: self,
236-
fn_name: fn_name.to_owned(),
237-
declared_inputs: WorkMap::new()
238-
};
239-
blk(p)
241+
pub fn prep<'a>(&'a self, fn_name: &'a str) -> Prep<'a> {
242+
Prep::new(self, fn_name)
240243
}
241-
}
242244
245+
pub fn with_prep<'a, T>(&'a self, fn_name: &'a str, blk: &fn(p: &mut Prep) -> T) -> T {
246+
let mut p = self.prep(fn_name);
247+
blk(&mut p)
248+
}
243249
244-
trait TPrep {
245-
fn declare_input(&mut self, kind:&str, name:&str, val:&str);
246-
fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool;
247-
fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool;
248-
fn exec<T:Send +
249-
Encodable<json::Encoder> +
250-
Decodable<json::Decoder>>( // FIXME(#5121)
251-
&self, blk: ~fn(&Exec) -> T) -> Work<T>;
252250
}
253251
254-
impl TPrep for Prep {
252+
impl<'self> Prep<'self> {
253+
fn new(ctxt: &'self Context, fn_name: &'self str) -> Prep<'self> {
254+
Prep {
255+
ctxt: ctxt,
256+
fn_name: fn_name,
257+
declared_inputs: WorkMap::new()
258+
}
259+
}
260+
}
261+
262+
impl<'self> Prep<'self> {
255263
fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
256264
self.declared_inputs.insert(WorkKey::new(kind, name),
257265
val.to_owned());
@@ -286,22 +294,28 @@ impl TPrep for Prep {
286294
}
287295

288296
fn exec<T:Send +
289-
Encodable<json::Encoder> +
290-
Decodable<json::Decoder>>( // FIXME(#5121)
291-
&self, blk: ~fn(&Exec) -> T) -> Work<T> {
297+
Encodable<json::Encoder> +
298+
Decodable<json::Decoder>>(
299+
&'self self, blk: ~fn(&Exec) -> T) -> T {
300+
self.exec_work(blk).unwrap()
301+
}
302+
303+
fn exec_work<T:Send +
304+
Encodable<json::Encoder> +
305+
Decodable<json::Decoder>>( // FIXME(#5121)
306+
&'self self, blk: ~fn(&Exec) -> T) -> Work<'self, T> {
292307
let mut bo = Some(blk);
293308

294309
let cached = do self.ctxt.db.read |db| {
295310
db.prepare(self.fn_name, &self.declared_inputs)
296311
};
297312

298-
match cached {
313+
let res = match cached {
299314
Some((ref disc_in, ref disc_out, ref res))
300-
if self.all_fresh("declared input",
301-
&self.declared_inputs) &&
302-
self.all_fresh("discovered input", disc_in) &&
303-
self.all_fresh("discovered output", disc_out) => {
304-
Work::new(@mut (*self).clone(), Left(json_decode(*res)))
315+
if self.all_fresh("declared input",&self.declared_inputs) &&
316+
self.all_fresh("discovered input", disc_in) &&
317+
self.all_fresh("discovered output", disc_out) => {
318+
Left(json_decode(*res))
305319
}
306320

307321
_ => {
@@ -318,16 +332,19 @@ impl TPrep for Prep {
318332
let v = blk(&exe);
319333
send_one(chan, (exe, v));
320334
}
321-
Work::new(@mut (*self).clone(), Right(port))
335+
Right(port)
322336
}
323-
}
337+
};
338+
Work::new(self, res)
324339
}
325340
}
326341

327-
impl<T:Send +
342+
impl<'self, T:Send +
328343
Encodable<json::Encoder> +
329-
Decodable<json::Decoder>> Work<T> { // FIXME(#5121)
330-
pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> {
344+
Decodable<json::Decoder>>
345+
Work<'self, T> { // FIXME(#5121)
346+
347+
pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
331348
Work { prep: p, res: Some(e) }
332349
}
333350

@@ -357,26 +374,22 @@ impl<T:Send +
357374
fn test() {
358375
use std::io::WriterUtil;
359376

360-
let db = RWARC(Database { db_filename: Path("db.json"),
361-
db_cache: TreeMap::new(),
362-
db_dirty: false });
363-
let lg = @mut Logger { a: () };
364-
let cfg = HashMap::new();
365-
let cx = @Context::new(db, lg, cfg);
366-
let w:Work<~str> = do cx.prep("test1") |prep| {
367-
let pth = Path("foo.c");
368-
{
369-
let file = io::file_writer(&pth, [io::Create]).unwrap();
370-
file.write_str("int main() { return 0; }");
371-
}
377+
let pth = Path("foo.c");
378+
{
379+
let r = io::file_writer(&pth, [io::Create]);
380+
r.get_ref().write_str("int main() { return 0; }");
381+
}
382+
383+
let cx = Context::new(RWARC(Database::new(Path("db.json"))),
384+
Logger::new(), HashMap::new());
372385

386+
let s = do cx.with_prep("test1") |prep| {
373387
prep.declare_input("file", pth.to_str(), digest_file(&pth));
374388
do prep.exec |_exe| {
375389
let out = Path("foo.o");
376390
run::process_status("gcc", [~"foo.c", ~"-o", out.to_str()]);
377391
out.to_str()
378392
}
379393
};
380-
let s = w.unwrap();
381394
io::println(s);
382395
}

0 commit comments

Comments
 (0)