]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_session/src/search_paths.rs
Auto merge of #93719 - scottmcm:core-as-2021-everywhere, r=Mark-Simulacrum
[rust.git] / compiler / rustc_session / src / search_paths.rs
index acb6c735e051ebc2cfb90b5310e07c7bc8208533..b6bde28233d243bc2ad86c0e5619111f3bc1d6d7 100644 (file)
@@ -15,22 +15,15 @@ pub struct SearchPath {
 /// doable, but very slow, because it involves calls to `file_name` and
 /// `extension` that are themselves slow.
 ///
-/// This type augments the `PathBuf` with an `Option<String>` containing the
+/// This type augments the `PathBuf` with an `String` containing the
 /// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
-/// `Option<String>` than the `PathBuf`. (It's an `Option` because
-/// `Path::file_name` can fail; if that happens then all subsequent checking
-/// will also fail, which is fine.)
+/// `String` than the `PathBuf`. (The filename must be valid UTF-8. If it's
+/// not, the entry should be skipped, because all Rust output files are valid
+/// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.)
 #[derive(Clone, Debug)]
 pub struct SearchPathFile {
     pub path: PathBuf,
-    pub file_name_str: Option<String>,
-}
-
-impl SearchPathFile {
-    fn new(path: PathBuf) -> SearchPathFile {
-        let file_name_str = path.file_name().and_then(|f| f.to_str()).map(|s| s.to_string());
-        SearchPathFile { path, file_name_str }
-    }
+    pub file_name_str: String,
 }
 
 #[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)]
@@ -85,7 +78,14 @@ fn new(kind: PathKind, dir: PathBuf) -> Self {
         // Get the files within the directory.
         let files = match std::fs::read_dir(&dir) {
             Ok(files) => files
-                .filter_map(|e| e.ok().map(|e| SearchPathFile::new(e.path())))
+                .filter_map(|e| {
+                    e.ok().and_then(|e| {
+                        e.file_name().to_str().map(|s| SearchPathFile {
+                            path: e.path(),
+                            file_name_str: s.to_string(),
+                        })
+                    })
+                })
                 .collect::<Vec<_>>(),
             Err(..) => vec![],
         };