Skip to content

Commit 504335a

Browse files
committed
libworkcache: Remove all uses of ~str from libworkcache.
1 parent 95e310a commit 504335a

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

src/libworkcache/lib.rs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,31 @@ use std::io::{File, MemWriter};
103103

104104
#[deriving(Clone, Eq, Encodable, Decodable, Ord, TotalOrd, TotalEq)]
105105
struct WorkKey {
106-
kind: ~str,
107-
name: ~str
106+
kind: StrBuf,
107+
name: StrBuf
108108
}
109109

110110
impl WorkKey {
111111
pub fn new(kind: &str, name: &str) -> WorkKey {
112112
WorkKey {
113-
kind: kind.to_owned(),
114-
name: name.to_owned(),
113+
kind: kind.to_strbuf(),
114+
name: name.to_strbuf(),
115115
}
116116
}
117117
}
118118

119-
// FIXME #8883: The key should be a WorkKey and not a ~str.
119+
// FIXME #8883: The key should be a WorkKey and not a StrBuf.
120120
// This is working around some JSON weirdness.
121121
#[deriving(Clone, Eq, Encodable, Decodable)]
122-
struct WorkMap(TreeMap<~str, KindMap>);
122+
struct WorkMap(TreeMap<StrBuf, KindMap>);
123123

124124
#[deriving(Clone, Eq, Encodable, Decodable)]
125-
struct KindMap(TreeMap<~str, ~str>);
125+
struct KindMap(TreeMap<StrBuf, StrBuf>);
126126

127127
impl WorkMap {
128128
fn new() -> WorkMap { WorkMap(TreeMap::new()) }
129129

130-
fn insert_work_key(&mut self, k: WorkKey, val: ~str) {
130+
fn insert_work_key(&mut self, k: WorkKey, val: StrBuf) {
131131
let WorkKey { kind, name } = k;
132132
let WorkMap(ref mut map) = *self;
133133
match map.find_mut(&name) {
@@ -142,7 +142,7 @@ impl WorkMap {
142142

143143
pub struct Database {
144144
db_filename: Path,
145-
db_cache: TreeMap<~str, ~str>,
145+
db_cache: TreeMap<StrBuf, StrBuf>,
146146
pub db_dirty: bool,
147147
}
148148

@@ -163,11 +163,11 @@ impl Database {
163163
pub fn prepare(&self,
164164
fn_name: &str,
165165
declared_inputs: &WorkMap)
166-
-> Option<(WorkMap, WorkMap, ~str)> {
166+
-> Option<(WorkMap, WorkMap, StrBuf)> {
167167
let k = json_encode(&(fn_name, declared_inputs));
168168
match self.db_cache.find(&k) {
169169
None => None,
170-
Some(v) => Some(json_decode(*v))
170+
Some(v) => Some(json_decode(v.as_slice()))
171171
}
172172
}
173173

@@ -188,7 +188,14 @@ impl Database {
188188
// FIXME #4330: This should have &mut self and should set self.db_dirty to false.
189189
fn save(&self) -> io::IoResult<()> {
190190
let mut f = File::create(&self.db_filename);
191-
self.db_cache.to_json().to_pretty_writer(&mut f)
191+
192+
// FIXME(pcwalton): Yuck.
193+
let mut new_db_cache = TreeMap::new();
194+
for (ref k, ref v) in self.db_cache.iter() {
195+
new_db_cache.insert((*k).to_owned(), (*v).to_owned());
196+
}
197+
198+
new_db_cache.to_json().to_pretty_writer(&mut f)
192199
}
193200

194201
fn load(&mut self) {
@@ -222,7 +229,7 @@ impl Drop for Database {
222229
}
223230
}
224231

225-
pub type FreshnessMap = TreeMap<~str,extern fn(&str,&str)->bool>;
232+
pub type FreshnessMap = TreeMap<StrBuf,extern fn(&str,&str)->bool>;
226233

227234
#[deriving(Clone)]
228235
pub struct Context {
@@ -253,11 +260,11 @@ enum Work<'a, T> {
253260
WorkFromTask(&'a Prep<'a>, Receiver<(Exec, T)>),
254261
}
255262

256-
fn json_encode<'a, T:Encodable<json::Encoder<'a>, io::IoError>>(t: &T) -> ~str {
263+
fn json_encode<'a, T:Encodable<json::Encoder<'a>, io::IoError>>(t: &T) -> StrBuf {
257264
let mut writer = MemWriter::new();
258265
let mut encoder = json::Encoder::new(&mut writer as &mut io::Writer);
259266
let _ = t.encode(&mut encoder);
260-
str::from_utf8(writer.unwrap().as_slice()).unwrap().to_owned()
267+
str::from_utf8(writer.unwrap().as_slice()).unwrap().to_strbuf()
261268
}
262269

263270
// FIXME(#5121)
@@ -308,19 +315,19 @@ impl Exec {
308315
dependency_val: &str) {
309316
debug!("Discovering input {} {} {}", dependency_kind, dependency_name, dependency_val);
310317
self.discovered_inputs.insert_work_key(WorkKey::new(dependency_kind, dependency_name),
311-
dependency_val.to_owned());
318+
dependency_val.to_strbuf());
312319
}
313320
pub fn discover_output(&mut self,
314321
dependency_kind: &str,
315322
dependency_name: &str,
316323
dependency_val: &str) {
317324
debug!("Discovering output {} {} {}", dependency_kind, dependency_name, dependency_val);
318325
self.discovered_outputs.insert_work_key(WorkKey::new(dependency_kind, dependency_name),
319-
dependency_val.to_owned());
326+
dependency_val.to_strbuf());
320327
}
321328

322329
// returns pairs of (kind, name)
323-
pub fn lookup_discovered_inputs(&self) -> Vec<(~str, ~str)> {
330+
pub fn lookup_discovered_inputs(&self) -> Vec<(StrBuf, StrBuf)> {
324331
let mut rs = vec![];
325332
let WorkMap(ref discovered_inputs) = self.discovered_inputs;
326333
for (k, v) in discovered_inputs.iter() {
@@ -342,7 +349,7 @@ impl<'a> Prep<'a> {
342349
}
343350
}
344351

345-
pub fn lookup_declared_inputs(&self) -> Vec<~str> {
352+
pub fn lookup_declared_inputs(&self) -> Vec<StrBuf> {
346353
let mut rs = vec![];
347354
let WorkMap(ref declared_inputs) = self.declared_inputs;
348355
for (_, v) in declared_inputs.iter() {
@@ -359,12 +366,11 @@ impl<'a> Prep<'a> {
359366
pub fn declare_input(&mut self, kind: &str, name: &str, val: &str) {
360367
debug!("Declaring input {} {} {}", kind, name, val);
361368
self.declared_inputs.insert_work_key(WorkKey::new(kind, name),
362-
val.to_owned());
369+
val.to_strbuf());
363370
}
364371

365-
fn is_fresh(&self, cat: &str, kind: &str,
366-
name: &str, val: &str) -> bool {
367-
let k = kind.to_owned();
372+
fn is_fresh(&self, cat: &str, kind: &str, name: &str, val: &str) -> bool {
373+
let k = kind.to_strbuf();
368374
let f = self.ctxt.freshness.deref().find(&k);
369375
debug!("freshness for: {}/{}/{}/{}", cat, kind, name, val)
370376
let fresh = match f {
@@ -384,7 +390,10 @@ impl<'a> Prep<'a> {
384390
for (k_name, kindmap) in map.iter() {
385391
let KindMap(ref kindmap_) = *kindmap;
386392
for (k_kind, v) in kindmap_.iter() {
387-
if ! self.is_fresh(cat, *k_kind, *k_name, *v) {
393+
if !self.is_fresh(cat,
394+
k_kind.as_slice(),
395+
k_name.as_slice(),
396+
v.as_slice()) {
388397
return false;
389398
}
390399
}
@@ -420,7 +429,7 @@ impl<'a> Prep<'a> {
420429
debug!("Cache hit!");
421430
debug!("Trying to decode: {:?} / {:?} / {}",
422431
disc_in, disc_out, *res);
423-
Work::from_value(json_decode(*res))
432+
Work::from_value(json_decode(res.as_slice()))
424433
}
425434

426435
_ => {
@@ -467,7 +476,7 @@ impl<'a, T:Send +
467476
&prep.declared_inputs,
468477
&exe.discovered_inputs,
469478
&exe.discovered_outputs,
470-
s);
479+
s.as_slice());
471480
v
472481
}
473482
}
@@ -484,18 +493,18 @@ fn test() {
484493

485494
// Create a path to a new file 'filename' in the directory in which
486495
// this test is running.
487-
fn make_path(filename: ~str) -> Path {
496+
fn make_path(filename: StrBuf) -> Path {
488497
let pth = os::self_exe_path().expect("workcache::test failed").with_filename(filename);
489498
if pth.exists() {
490499
fs::unlink(&pth).unwrap();
491500
}
492501
return pth;
493502
}
494503

495-
let pth = make_path("foo.c".to_owned());
504+
let pth = make_path("foo.c".to_strbuf());
496505
File::create(&pth).write(bytes!("int main() { return 0; }")).unwrap();
497506

498-
let db_path = make_path("db.json".to_owned());
507+
let db_path = make_path("db.json".to_strbuf());
499508

500509
let cx = Context::new(Arc::new(RWLock::new(Database::new(db_path))),
501510
Arc::new(TreeMap::new()));
@@ -511,7 +520,7 @@ fn test() {
511520
// FIXME (#9639): This needs to handle non-utf8 paths
512521
prep.declare_input("file", pth.as_str().unwrap(), file_content);
513522
prep.exec(proc(_exe) {
514-
let out = make_path("foo.o".to_owned());
523+
let out = make_path("foo.o".to_strbuf());
515524
let compiler = if cfg!(windows) {"gcc"} else {"cc"};
516525
// FIXME (#9639): This needs to handle non-utf8 paths
517526
Process::status(compiler, [pth.as_str().unwrap().to_owned(),

0 commit comments

Comments
 (0)