@@ -124,6 +124,15 @@ struct Database {
124
124
}
125
125
126
126
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
+
127
136
pub fn prepare ( & self ,
128
137
fn_name : & str ,
129
138
declared_inputs : & WorkMap )
@@ -156,22 +165,26 @@ struct Logger {
156
165
}
157
166
158
167
impl Logger {
168
+
169
+ pub fn new ( ) -> Logger {
170
+ Logger { a : ( ) }
171
+ }
172
+
159
173
pub fn info ( & self , i : & str ) {
160
174
io:: println ( ~"workcache: " + i);
161
175
}
162
176
}
163
177
164
178
struct Context {
165
179
db: RWARC<Database>,
166
- logger: @mut Logger,
180
+ logger: Logger,
167
181
cfg: json::Object,
168
182
freshness: TreeMap<~str,@fn(&str,&str)->bool>
169
183
}
170
184
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,
175
188
declared_inputs: WorkMap,
176
189
}
177
190
@@ -180,8 +193,8 @@ struct Exec {
180
193
discovered_outputs: WorkMap
181
194
}
182
195
183
- struct Work<T> {
184
- prep: @mut Prep,
196
+ struct Work<'self, T> {
197
+ prep: &'self Prep<'self> ,
185
198
res: Option<Either<T,PortOne<(Exec,T)>>>
186
199
}
187
200
@@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str {
215
228
}
216
229
217
230
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 {
220
233
Context {
221
234
db: db,
222
235
logger: lg,
@@ -225,33 +238,28 @@ impl Context {
225
238
}
226
239
}
227
240
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)
240
243
}
241
- }
242
244
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
+ }
243
249
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>;
252
250
}
253
251
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> {
255
263
fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
256
264
self.declared_inputs.insert(WorkKey::new(kind, name),
257
265
val.to_owned());
@@ -286,22 +294,28 @@ impl TPrep for Prep {
286
294
}
287
295
288
296
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 > {
292
307
let mut bo = Some ( blk) ;
293
308
294
309
let cached = do self . ctxt. db. read |db| {
295
310
db. prepare( self . fn_name, & self . declared_inputs)
296
311
} ;
297
312
298
- match cached {
313
+ let res = match cached {
299
314
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) )
305
319
}
306
320
307
321
_ => {
@@ -318,16 +332,19 @@ impl TPrep for Prep {
318
332
let v = blk( & exe) ;
319
333
send_one( chan, ( exe, v) ) ;
320
334
}
321
- Work :: new ( @ mut ( * self ) . clone ( ) , Right ( port) )
335
+ Right ( port)
322
336
}
323
- }
337
+ } ;
338
+ Work :: new( self , res)
324
339
}
325
340
}
326
341
327
- impl <T : Send +
342
+ impl <' self , T : Send +
328
343
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 > {
331
348
Work { prep : p, res : Some ( e) }
332
349
}
333
350
@@ -357,26 +374,22 @@ impl<T:Send +
357
374
fn test( ) {
358
375
use std:: io:: WriterUtil ;
359
376
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( ) ) ;
372
385
386
+ let s = do cx. with_prep( "test1" ) |prep| {
373
387
prep. declare_input( "file" , pth. to_str( ) , digest_file( & pth) ) ;
374
388
do prep. exec |_exe| {
375
389
let out = Path ( "foo.o" ) ;
376
390
run:: process_status( "gcc" , [ ~"foo. c", ~" -o", out. to_str( ) ] ) ;
377
391
out. to_str( )
378
392
}
379
393
} ;
380
- let s = w. unwrap( ) ;
381
394
io:: println( s) ;
382
395
}
0 commit comments