Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Perform some housekeeping #95

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
})
}

// 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, since the previous link was invalid

pub fn src_url(&self, span: &Span) -> AResult<String> {
// FIXME would be nice not to do this every time.
let path_prefix = &self.loader.abs_path_prefix();
Expand Down Expand Up @@ -535,15 +535,8 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
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{}",
Expand Down
2 changes: 1 addition & 1 deletion src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 15 additions & 10 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ pub fn read_analyis_incremental<L: AnalysisLoader>(
}

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.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to get rid of defining the same block twice for two cases, is this okay?

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))));
}
Expand Down Expand Up @@ -105,14 +103,21 @@ fn ignore_data(file_name: &str, crate_blacklist: Blacklist) -> bool {
false
}

fn read_file_contents(path: &Path) -> Result<String, ::std::io::Error> {
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<Analysis> {
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| {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separated into read_file_contents to catch a possible error and report it, however if we were to ignore warning about the error, this could be further simplified with ok()?s - should I change that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, seems good to acknowledge the error

info!("couldn't read file: {}", err);
Err(err)
}).ok()?;
let s = ::rustc_serialize::json::decode(&buf);

let d = t.elapsed();
Expand Down
12 changes: 4 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! option_try(
Copy link
Collaborator Author

@Xanewok Xanewok Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl Try for Option has landed! 🎉

($e:expr) => (match $e { Some(e) => e, None => return None })
);

#[cfg(unix)]
pub fn get_resident() -> Option<usize> {
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::<usize>().ok());
f.read_to_string(&mut contents).ok()?;
let s = contents.split_whitespace().nth(field)?;
let npages = s.parse::<usize>().ok()?;
Some(npages * 4096)
}

Expand Down