From 183673655f72250ee05ab1c5864feeae33a3c662 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 26 Apr 2020 17:58:56 +0800 Subject: Simpify code --- crates/ra_proc_macro_srv/src/dylib.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'crates/ra_proc_macro_srv/src/dylib.rs') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 99c83481a..209f61493 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -109,24 +109,19 @@ impl ProcMacroLibraryLibloading { } } -type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; - pub struct Expander { - inner: ProcMacroLibraryImpl, + inner: ProcMacroLibraryLibloading, } impl Expander { - pub fn new(lib: &Path) -> Result { + pub fn new(lib: &Path) -> io::Result { // 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 @@ impl Expander { } } +/// Copy the dylib to temp directory to prevent locking in Windows #[cfg(windows)] -fn copy_to_temp_dir(path: &Path) -> io::Result { +fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { 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 { } #[cfg(unix)] -fn copy_to_temp_dir(path: &Path) -> io::Result { +fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { Ok(path.to_path_buf()) } -- cgit v1.2.3