about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2013-07-17 21:12:36 -0700
committerGraydon Hoare <graydon@mozilla.com>2013-07-23 15:23:02 -0700
commit31c180e5f5da0e27c60a3bf02da182f19c66cf8f (patch)
tree8d901b850652b02677a0fe3fbe1fc997ae1811e5 /src/libextra
parent1d9181bd760cb89171e892204338bb64275eb4e5 (diff)
downloadrust-31c180e5f5da0e27c60a3bf02da182f19c66cf8f.tar.gz
rust-31c180e5f5da0e27c60a3bf02da182f19c66cf8f.zip
extra: clean up workcache to use & in place of @ most places.
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/workcache.rs129
1 files 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 @@ impl Logger {
 
 struct Context {
     db: RWARC<Database>,
-    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<T> {
-    prep: @mut Prep,
+struct Work<'self, T> {
+    prep: &'self Prep<'self>,
     res: Option<Either<T,PortOne<(Exec,T)>>>
 }
 
@@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str {
 }
 
 impl Context {
-    pub fn new(db: RWARC<Database>, lg: @mut Logger, cfg: json::Object)
-               -> Context {
+
+    pub fn new(db: RWARC<Database>, lg: Logger, cfg: json::Object) -> Context {
         Context {
             db: db,
             logger: lg,
@@ -225,33 +238,28 @@ impl Context {
         }
     }
 
-    pub fn prep<T:Send +
-                  Encodable<json::Encoder> +
-                  Decodable<json::Decoder>>(@self, // FIXME(#5121)
-                                            fn_name:&str,
-                                            blk: &fn(@mut Prep)->Work<T>)
-                                            -> Work<T> {
-        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<T:Send +
-              Encodable<json::Encoder> +
-              Decodable<json::Decoder>>( // FIXME(#5121)
-        &self, blk: ~fn(&Exec) -> T) -> Work<T>;
 }
 
-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 @@ impl TPrep for Prep {
     }
 
     fn exec<T:Send +
-              Encodable<json::Encoder> +
-              Decodable<json::Decoder>>( // FIXME(#5121)
-            &self, blk: ~fn(&Exec) -> T) -> Work<T> {
+        Encodable<json::Encoder> +
+        Decodable<json::Decoder>>(
+            &'self self, blk: ~fn(&Exec) -> T) -> T {
+        self.exec_work(blk).unwrap()
+    }
+
+    fn exec_work<T:Send +
+        Encodable<json::Encoder> +
+        Decodable<json::Decoder>>( // 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 @@ impl TPrep for Prep {
                     let v = blk(&exe);
                     send_one(chan, (exe, v));
                 }
-                Work::new(@mut (*self).clone(), Right(port))
+                Right(port)
             }
-        }
+        };
+        Work::new(self, res)
     }
 }
 
-impl<T:Send +
+impl<'self, T:Send +
        Encodable<json::Encoder> +
-       Decodable<json::Decoder>> Work<T> { // FIXME(#5121)
-    pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> {
+       Decodable<json::Decoder>>
+    Work<'self, T> { // FIXME(#5121)
+
+    pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
         Work { prep: p, res: Some(e) }
     }
 
@@ -357,19 +374,16 @@ impl<T:Send +
 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);
 }