From 31c180e5f5da0e27c60a3bf02da182f19c66cf8f Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 17 Jul 2013 21:12:36 -0700 Subject: [PATCH] extra: clean up workcache to use & in place of @ most places. --- src/libextra/workcache.rs | 129 +++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 58 deletions(-) diff --git a/src/libextra/workcache.rs b/src/libextra/workcache.rs index 41a65706741..f7585ca03ba 100644 --- a/src/libextra/workcache.rs +++ b/src/libextra/workcache.rs @@ -124,6 +124,15 @@ struct Database { } impl Database { + + pub fn new(p: Path) -> Database { + Database { + db_filename: p, + db_cache: TreeMap::new(), + db_dirty: false + } + } + pub fn prepare(&self, fn_name: &str, declared_inputs: &WorkMap) @@ -156,6 +165,11 @@ struct Logger { } impl Logger { + + pub fn new() -> Logger { + Logger { a: () } + } + pub fn info(&self, i: &str) { io::println(~"workcache: " + i); } @@ -163,15 +177,14 @@ pub fn info(&self, i: &str) { struct Context { db: RWARC, - logger: @mut Logger, + logger: Logger, cfg: json::Object, freshness: TreeMap<~str,@fn(&str,&str)->bool> } -#[deriving(Clone)] -struct Prep { - ctxt: @Context, - fn_name: ~str, +struct Prep<'self> { + ctxt: &'self Context, + fn_name: &'self str, declared_inputs: WorkMap, } @@ -180,8 +193,8 @@ struct Exec { discovered_outputs: WorkMap } -struct Work { - prep: @mut Prep, +struct Work<'self, T> { + prep: &'self Prep<'self>, res: Option>> } @@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str { } impl Context { - pub fn new(db: RWARC, lg: @mut Logger, cfg: json::Object) - -> Context { + + pub fn new(db: RWARC, lg: Logger, cfg: json::Object) -> Context { Context { db: db, logger: lg, @@ -225,33 +238,28 @@ pub fn new(db: RWARC, lg: @mut Logger, cfg: json::Object) } } - pub fn prep + - Decodable>(@self, // FIXME(#5121) - fn_name:&str, - blk: &fn(@mut Prep)->Work) - -> Work { - let p = @mut Prep { - ctxt: self, - fn_name: fn_name.to_owned(), - declared_inputs: WorkMap::new() - }; - blk(p) + pub fn prep<'a>(&'a self, fn_name: &'a str) -> Prep<'a> { + Prep::new(self, fn_name) } -} + pub fn with_prep<'a, T>(&'a self, fn_name: &'a str, blk: &fn(p: &mut Prep) -> T) -> T { + let mut p = self.prep(fn_name); + blk(&mut p) + } -trait TPrep { - fn declare_input(&mut self, kind:&str, name:&str, val:&str); - fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool; - fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool; - fn exec + - Decodable>( // FIXME(#5121) - &self, blk: ~fn(&Exec) -> T) -> Work; } -impl TPrep for Prep { +impl<'self> Prep<'self> { + fn new(ctxt: &'self Context, fn_name: &'self str) -> Prep<'self> { + Prep { + ctxt: ctxt, + fn_name: fn_name, + declared_inputs: WorkMap::new() + } + } +} + +impl<'self> Prep<'self> { fn declare_input(&mut self, kind:&str, name:&str, val:&str) { self.declared_inputs.insert(WorkKey::new(kind, name), val.to_owned()); @@ -286,22 +294,28 @@ fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool { } fn exec + - Decodable>( // FIXME(#5121) - &self, blk: ~fn(&Exec) -> T) -> Work { + Encodable + + Decodable>( + &'self self, blk: ~fn(&Exec) -> T) -> T { + self.exec_work(blk).unwrap() + } + + fn exec_work + + Decodable>( // FIXME(#5121) + &'self self, blk: ~fn(&Exec) -> T) -> Work<'self, T> { let mut bo = Some(blk); let cached = do self.ctxt.db.read |db| { db.prepare(self.fn_name, &self.declared_inputs) }; - match cached { + let res = match cached { Some((ref disc_in, ref disc_out, ref res)) - if self.all_fresh("declared input", - &self.declared_inputs) && - self.all_fresh("discovered input", disc_in) && - self.all_fresh("discovered output", disc_out) => { - Work::new(@mut (*self).clone(), Left(json_decode(*res))) + if self.all_fresh("declared input",&self.declared_inputs) && + self.all_fresh("discovered input", disc_in) && + self.all_fresh("discovered output", disc_out) => { + Left(json_decode(*res)) } _ => { @@ -318,16 +332,19 @@ fn exec + - Decodable> Work { // FIXME(#5121) - pub fn new(p: @mut Prep, e: Either>) -> Work { + Decodable> + Work<'self, T> { // FIXME(#5121) + + pub fn new(p: &'self Prep<'self>, e: Either>) -> Work<'self, T> { Work { prep: p, res: Some(e) } } @@ -357,19 +374,16 @@ pub fn unwrap(self) -> T { fn test() { use std::io::WriterUtil; - let db = RWARC(Database { db_filename: Path("db.json"), - db_cache: TreeMap::new(), - db_dirty: false }); - let lg = @mut Logger { a: () }; - let cfg = HashMap::new(); - let cx = @Context::new(db, lg, cfg); - let w:Work<~str> = do cx.prep("test1") |prep| { - let pth = Path("foo.c"); - { - let file = io::file_writer(&pth, [io::Create]).unwrap(); - file.write_str("int main() { return 0; }"); - } + let pth = Path("foo.c"); + { + let r = io::file_writer(&pth, [io::Create]); + r.get_ref().write_str("int main() { return 0; }"); + } + + let cx = Context::new(RWARC(Database::new(Path("db.json"))), + Logger::new(), HashMap::new()); + let s = do cx.with_prep("test1") |prep| { prep.declare_input("file", pth.to_str(), digest_file(&pth)); do prep.exec |_exe| { let out = Path("foo.o"); @@ -377,6 +391,5 @@ fn test() { out.to_str() } }; - let s = w.unwrap(); io::println(s); } -- GitLab