diff options
author | Edwin Cheng <[email protected]> | 2020-04-26 10:58:56 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-04-26 10:58:56 +0100 |
commit | 183673655f72250ee05ab1c5864feeae33a3c662 (patch) | |
tree | 96c0ee813095ff18488ea88d2c07d8996d7b9405 /crates/ra_proc_macro_srv/src/dylib.rs | |
parent | fb996cae6bf5bb5f60176a4a190311d4363b3e10 (diff) |
Simpify code
Diffstat (limited to 'crates/ra_proc_macro_srv/src/dylib.rs')
-rw-r--r-- | crates/ra_proc_macro_srv/src/dylib.rs | 20 |
1 files changed, 8 insertions, 12 deletions
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 { | |||
109 | } | 109 | } |
110 | } | 110 | } |
111 | 111 | ||
112 | type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; | ||
113 | |||
114 | pub struct Expander { | 112 | pub struct Expander { |
115 | inner: ProcMacroLibraryImpl, | 113 | inner: ProcMacroLibraryLibloading, |
116 | } | 114 | } |
117 | 115 | ||
118 | impl Expander { | 116 | impl Expander { |
119 | pub fn new(lib: &Path) -> Result<Expander, String> { | 117 | pub fn new(lib: &Path) -> io::Result<Expander> { |
120 | // Some libraries for dynamic loading require canonicalized path even when it is | 118 | // Some libraries for dynamic loading require canonicalized path even when it is |
121 | // already absolute | 119 | // already absolute |
122 | let lib = lib | 120 | let lib = lib.canonicalize()?; |
123 | .canonicalize() | ||
124 | .unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err)); | ||
125 | 121 | ||
126 | // Copy the dylib to temp directory to prevent locking in Windows | 122 | let lib = ensure_file_with_lock_free_access(&lib)?; |
127 | let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?; | ||
128 | 123 | ||
129 | let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; | 124 | let library = ProcMacroLibraryLibloading::open(&lib)?; |
130 | 125 | ||
131 | Ok(Expander { inner: library }) | 126 | Ok(Expander { inner: library }) |
132 | } | 127 | } |
@@ -199,8 +194,9 @@ impl Expander { | |||
199 | } | 194 | } |
200 | } | 195 | } |
201 | 196 | ||
197 | /// Copy the dylib to temp directory to prevent locking in Windows | ||
202 | #[cfg(windows)] | 198 | #[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> { |
204 | let mut to = std::env::temp_dir(); | 200 | let mut to = std::env::temp_dir(); |
205 | let file_name = path.file_name().ok_or_else(|| { | 201 | let file_name = path.file_name().ok_or_else(|| { |
206 | io::Error::new( | 202 | io::Error::new( |
@@ -215,6 +211,6 @@ fn copy_to_temp_dir(path: &Path) -> io::Result<PathBuf> { | |||
215 | } | 211 | } |
216 | 212 | ||
217 | #[cfg(unix)] | 213 | #[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> { |
219 | Ok(path.to_path_buf()) | 215 | Ok(path.to_path_buf()) |
220 | } | 216 | } |