From d6a528d1ad21251e69d80226d1bde99fa08334ef Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Thu, 5 Oct 2017 18:32:43 +0200 Subject: [PATCH] Perform some housekeeping --- src/lib.rs | 11 ++--------- src/lowering.rs | 2 +- src/raw.rs | 25 +++++++++++++++---------- src/util.rs | 12 ++++-------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 24e518d..fde7b31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -446,7 +446,7 @@ impl AnalysisHost { }) } - // e.g., https://github.com/rust-lang/rust/blob/master/src/libcollections/string.rs#L261-L263 + // e.g., https://github.com/rust-lang/rust/blob/master/src/liballoc/string.rs#L261-L263 pub fn src_url(&self, span: &Span) -> AResult { // FIXME would be nice not to do this every time. let path_prefix = &self.loader.abs_path_prefix(); @@ -535,15 +535,8 @@ impl AnalysisHost { return None; } - let path_prefix = match path_prefix { - Some(pp) => pp, - None => return None, - }; let file_path = &def.span.file; - let file_path = match file_path.strip_prefix(&path_prefix) { - Ok(p) => p, - Err(_) => return None, - }; + let file_path = file_path.strip_prefix(path_prefix?).ok()?; Some(format!( "{}/{}#L{}-L{}", diff --git a/src/lowering.rs b/src/lowering.rs index 6067231..f743ac1 100644 --- a/src/lowering.rs +++ b/src/lowering.rs @@ -341,7 +341,7 @@ impl CrateReader { return NULL; } // We build an id by looking up the local crate number into a global crate number and using - // that for the high order bits, then use the index for the least significant bits. + // that for the high order bits, then use the index for the least significant bits. let krate = self.crate_map[id.krate as usize] as u64; diff --git a/src/raw.rs b/src/raw.rs index 2d65cfd..783a48b 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -70,12 +70,10 @@ pub fn read_analyis_incremental( } match timestamps.get(&path) { - Some(t) => if time > t { - read_crate_data(&path) - .map(|a| result.push(Crate::new(a, *time, Some(path)))); - }, - // A crate we've never seen before. - None => { + // We have fresher data than what we can read. + Some(t) => if time <= t {}, + // We have old data or it's a crate we've never seen before. + _ => { read_crate_data(&path) .map(|a| result.push(Crate::new(a, *time, Some(path)))); } @@ -105,14 +103,21 @@ fn ignore_data(file_name: &str, crate_blacklist: Blacklist) -> bool { false } +fn read_file_contents(path: &Path) -> Result { + let mut file = File::open(&path)?; + let mut buf = String::new(); + file.read_to_string(&mut buf)?; + Ok(buf) +} fn read_crate_data(path: &Path) -> Option { trace!("read_crate_data {:?}", path); - // TODO unwraps let t = Instant::now(); - let mut file = File::open(&path).unwrap(); - let mut buf = String::new(); - file.read_to_string(&mut buf).unwrap(); + + let buf = read_file_contents(path).or_else(|err| { + info!("couldn't read file: {}", err); + Err(err) + }).ok()?; let s = ::rustc_serialize::json::decode(&buf); let d = t.elapsed(); diff --git a/src/util.rs b/src/util.rs index c077440..d5e8a82 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,21 +6,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -macro_rules! option_try( - ($e:expr) => (match $e { Some(e) => e, None => return None }) -); - #[cfg(unix)] pub fn get_resident() -> Option { use std::fs::File; use std::io::Read; let field = 1; - let mut f = option_try!(File::open("/proc/self/statm").ok()); + let mut f = File::open("/proc/self/statm").ok()?; let mut contents = String::new(); - option_try!(f.read_to_string(&mut contents).ok()); - let s = option_try!(contents.split_whitespace().nth(field)); - let npages = option_try!(s.parse::().ok()); + f.read_to_string(&mut contents).ok()?; + let s = contents.split_whitespace().nth(field)?; + let npages = s.parse::().ok()?; Some(npages * 4096) }