mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Simpify code
This commit is contained in:
parent
fb996cae6b
commit
183673655f
2 changed files with 12 additions and 16 deletions
|
@ -109,24 +109,19 @@ impl ProcMacroLibraryLibloading {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProcMacroLibraryImpl = ProcMacroLibraryLibloading;
|
|
||||||
|
|
||||||
pub struct Expander {
|
pub struct Expander {
|
||||||
inner: ProcMacroLibraryImpl,
|
inner: ProcMacroLibraryLibloading,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expander {
|
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
|
// Some libraries for dynamic loading require canonicalized path even when it is
|
||||||
// already absolute
|
// already absolute
|
||||||
let lib = lib
|
let lib = lib.canonicalize()?;
|
||||||
.canonicalize()
|
|
||||||
.unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err));
|
|
||||||
|
|
||||||
// Copy the dylib to temp directory to prevent locking in Windows
|
let lib = ensure_file_with_lock_free_access(&lib)?;
|
||||||
let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?;
|
|
||||||
|
|
||||||
let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?;
|
let library = ProcMacroLibraryLibloading::open(&lib)?;
|
||||||
|
|
||||||
Ok(Expander { inner: library })
|
Ok(Expander { inner: library })
|
||||||
}
|
}
|
||||||
|
@ -199,8 +194,9 @@ impl Expander {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copy the dylib to temp directory to prevent locking in Windows
|
||||||
#[cfg(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 mut to = std::env::temp_dir();
|
||||||
let file_name = path.file_name().ok_or_else(|| {
|
let file_name = path.file_name().ok_or_else(|| {
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
|
@ -215,6 +211,6 @@ fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[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())
|
Ok(path.to_path_buf())
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ use proc_macro::bridge::client::TokenStream;
|
||||||
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
|
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{hash_map::Entry, HashMap},
|
collections::{hash_map::Entry, HashMap},
|
||||||
fs::metadata,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
|
@ -50,9 +50,9 @@ impl ProcMacroSrv {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
|
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
|
||||||
let time = metadata(path)
|
let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| {
|
||||||
.and_then(|it| it.modified())
|
format!("Failed to get file metadata for {}: {:?}", path.display(), err)
|
||||||
.map_err(|err| format!("Failed to file metadata for {}: {:?}", path.display(), err))?;
|
})?;
|
||||||
|
|
||||||
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
|
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
|
||||||
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {
|
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue