Skip to content

Commit 1836736

Browse files
committed
Simpify code
1 parent fb996ca commit 1836736

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

crates/ra_proc_macro_srv/src/dylib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,19 @@ impl ProcMacroLibraryLibloading {
109109
}
110110
}
111111

112-
type ProcMacroLibraryImpl = ProcMacroLibraryLibloading;
113-
114112
pub struct Expander {
115-
inner: ProcMacroLibraryImpl,
113+
inner: ProcMacroLibraryLibloading,
116114
}
117115

118116
impl Expander {
119-
pub fn new(lib: &Path) -> Result<Expander, String> {
117+
pub fn new(lib: &Path) -> io::Result<Expander> {
120118
// Some libraries for dynamic loading require canonicalized path even when it is
121119
// already absolute
122-
let lib = lib
123-
.canonicalize()
124-
.unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err));
120+
let lib = lib.canonicalize()?;
125121

126-
// Copy the dylib to temp directory to prevent locking in Windows
127-
let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?;
122+
let lib = ensure_file_with_lock_free_access(&lib)?;
128123

129-
let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?;
124+
let library = ProcMacroLibraryLibloading::open(&lib)?;
130125

131126
Ok(Expander { inner: library })
132127
}
@@ -199,8 +194,9 @@ impl Expander {
199194
}
200195
}
201196

197+
/// Copy the dylib to temp directory to prevent locking in Windows
202198
#[cfg(windows)]
203-
fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
199+
fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
204200
let mut to = std::env::temp_dir();
205201
let file_name = path.file_name().ok_or_else(|| {
206202
io::Error::new(
@@ -215,6 +211,6 @@ fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
215211
}
216212

217213
#[cfg(unix)]
218-
fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
214+
fn ensure_file_with_lock_free_access(path: &Path) -> io::Result<PathBuf> {
219215
Ok(path.to_path_buf())
220216
}

crates/ra_proc_macro_srv/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use proc_macro::bridge::client::TokenStream;
2323
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
2424
use std::{
2525
collections::{hash_map::Entry, HashMap},
26-
fs::metadata,
26+
fs,
2727
path::{Path, PathBuf},
2828
time::SystemTime,
2929
};
@@ -50,9 +50,9 @@ impl ProcMacroSrv {
5050
}
5151

5252
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
53-
let time = metadata(path)
54-
.and_then(|it| it.modified())
55-
.map_err(|err| format!("Failed to file metadata for {}: {:?}", path.display(), err))?;
53+
let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| {
54+
format!("Failed to get file metadata for {}: {:?}", path.display(), err)
55+
})?;
5656

5757
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
5858
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {

0 commit comments

Comments
 (0)