Skip to content

Commit 95a6262

Browse files
committedDec 11, 2018
Replace FileSearch::for_each_lib_search_path with search_paths.
Returning an iterator leads to nicer code all around.

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed
 

‎src/librustc/session/filesearch.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,19 @@ pub enum FileMatch {
2929
// A module for searching for libraries
3030

3131
pub struct FileSearch<'a> {
32-
pub sysroot: &'a Path,
33-
pub triple: &'a str,
34-
pub search_paths: &'a [SearchPath],
35-
pub tlib_path: &'a SearchPath,
36-
pub kind: PathKind,
32+
sysroot: &'a Path,
33+
triple: &'a str,
34+
search_paths: &'a [SearchPath],
35+
tlib_path: &'a SearchPath,
36+
kind: PathKind,
3737
}
3838

3939
impl<'a> FileSearch<'a> {
40-
pub fn for_each_lib_search_path<F>(&self, mut f: F) where
41-
F: FnMut(&SearchPath)
42-
{
43-
let iter = self.search_paths.iter().filter(|sp| sp.kind.matches(self.kind));
44-
for search_path in iter {
45-
f(search_path);
46-
}
47-
48-
f(self.tlib_path);
40+
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
41+
let kind = self.kind;
42+
self.search_paths.iter()
43+
.filter(move |sp| sp.kind.matches(kind))
44+
.chain(std::iter::once(self.tlib_path))
4945
}
5046

5147
pub fn get_lib_path(&self) -> PathBuf {
@@ -55,7 +51,7 @@ impl<'a> FileSearch<'a> {
5551
pub fn search<F>(&self, mut pick: F)
5652
where F: FnMut(&Path, PathKind) -> FileMatch
5753
{
58-
self.for_each_lib_search_path(|search_path| {
54+
for search_path in self.search_paths() {
5955
debug!("searching {}", search_path.dir.display());
6056
fn is_rlib(p: &Path) -> bool {
6157
p.extension() == Some("rlib".as_ref())
@@ -78,7 +74,7 @@ impl<'a> FileSearch<'a> {
7874
}
7975
}
8076
}
81-
});
77+
}
8278
}
8379

8480
pub fn new(sysroot: &'a Path,
@@ -97,13 +93,11 @@ impl<'a> FileSearch<'a> {
9793
}
9894
}
9995

100-
// Returns a list of directories where target-specific dylibs might be located.
101-
pub fn get_dylib_search_paths(&self) -> Vec<PathBuf> {
102-
let mut paths = Vec::new();
103-
self.for_each_lib_search_path(|search_path| {
104-
paths.push(search_path.dir.to_path_buf());
105-
});
106-
paths
96+
// Returns just the directories within the search paths.
97+
pub fn search_path_dirs(&self) -> Vec<PathBuf> {
98+
self.search_paths()
99+
.map(|sp| sp.dir.to_path_buf())
100+
.collect()
107101
}
108102

109103
// Returns a list of directories where target-specific tool binaries are located.

‎src/librustc_codegen_llvm/back/link.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,7 @@ fn link_binary_output(sess: &Session,
212212
}
213213

214214
fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
215-
let mut search = Vec::new();
216-
sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|search_path| {
217-
search.push(search_path.dir.to_path_buf());
218-
});
219-
220-
search
215+
sess.target_filesearch(PathKind::Native).search_path_dirs()
221216
}
222217

223218
fn archive_config<'a>(sess: &'a Session,
@@ -1067,12 +1062,13 @@ fn link_args(cmd: &mut dyn Linker,
10671062
fn add_local_native_libraries(cmd: &mut dyn Linker,
10681063
sess: &Session,
10691064
codegen_results: &CodegenResults) {
1070-
sess.target_filesearch(PathKind::All).for_each_lib_search_path(|search_path| {
1065+
let filesearch = sess.target_filesearch(PathKind::All);
1066+
for search_path in filesearch.search_paths() {
10711067
match search_path.kind {
10721068
PathKind::Framework => { cmd.framework_path(&search_path.dir); }
10731069
_ => { cmd.include_path(&fix_windows_verbatim_for_gcc(&search_path.dir)); }
10741070
}
1075-
});
1071+
}
10761072

10771073
let relevant_libs = codegen_results.crate_info.used_libraries.iter().filter(|l| {
10781074
relevant_lib(sess, l)

‎src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ where
975975
let mut old_path = OsString::new();
976976
if cfg!(windows) {
977977
old_path = env::var_os("PATH").unwrap_or(old_path);
978-
let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
978+
let mut new_path = sess.host_filesearch(PathKind::All).search_path_dirs();
979979
for path in env::split_paths(&old_path) {
980980
if !new_path.contains(&path) {
981981
new_path.push(path);

0 commit comments

Comments
 (0)
Please sign in to comment.