]> git.lizzy.rs Git - rust.git/commitdiff
Simpify code
authorEdwin Cheng <edwin0cheng@gmail.com>
Sun, 26 Apr 2020 09:58:56 +0000 (17:58 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Sun, 26 Apr 2020 09:58:56 +0000 (17:58 +0800)
crates/ra_proc_macro_srv/src/dylib.rs
crates/ra_proc_macro_srv/src/lib.rs

index 99c83481a0437391d8a6b7d6bc9d7724e908dfb9..209f614932e884ed5cd193e065c466ebb2ab8bd6 100644 (file)
@@ -109,24 +109,19 @@ fn open(file: &Path) -> io::Result<Self> {
     }
 }
 
-type ProcMacroLibraryImpl = ProcMacroLibraryLibloading;
-
 pub struct Expander {
-    inner: ProcMacroLibraryImpl,
+    inner: ProcMacroLibraryLibloading,
 }
 
 impl Expander {
-    pub fn new(lib: &Path) -> Result<Expander, String> {
+    pub fn new(lib: &Path) -> io::Result<Expander> {
         // Some libraries for dynamic loading require canonicalized path even when it is
         // already absolute
-        let lib = lib
-            .canonicalize()
-            .unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err));
+        let lib = lib.canonicalize()?;
 
-        // Copy the dylib to temp directory to prevent locking in Windows
-        let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?;
+        let lib = ensure_file_with_lock_free_access(&lib)?;
 
-        let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?;
+        let library = ProcMacroLibraryLibloading::open(&lib)?;
 
         Ok(Expander { inner: library })
     }
@@ -199,8 +194,9 @@ pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
     }
 }
 
+/// Copy the dylib to temp directory to prevent locking in Windows
 #[cfg(windows)]
-fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
+fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
     let mut to = std::env::temp_dir();
     let file_name = path.file_name().ok_or_else(|| {
         io::Error::new(
@@ -215,6 +211,6 @@ fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
 }
 
 #[cfg(unix)]
-fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
+fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
     Ok(path.to_path_buf())
 }
index 121f531fd530b1a3e30010dca167279daf467ba8..922bb84bbfcda92336fcc3f9b35536900547159b 100644 (file)
@@ -23,7 +23,7 @@
 use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
 use std::{
     collections::{hash_map::Entry, HashMap},
-    fs::metadata,
+    fs,
     path::{Path, PathBuf},
     time::SystemTime,
 };
@@ -50,9 +50,9 @@ pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult,
     }
 
     fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
-        let time = metadata(path)
-            .and_then(|it| it.modified())
-            .map_err(|err| format!("Failed to file metadata for {}: {:?}", path.display(), err))?;
+        let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| {
+            format!("Failed to get file metadata for {}: {:?}", path.display(), err)
+        })?;
 
         Ok(match self.expanders.entry((path.to_path_buf(), time)) {
             Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {