]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_metadata/dynamic_lib.rs
Auto merge of #67996 - JohnTitor:clippy-up, r=JohnTitor
[rust.git] / src / librustc_metadata / dynamic_lib.rs
index 4c279361ff5e1c657b2781670e53634a948b8e55..fa4983d8a815ec9a5ed639d031d2e2415732f8d3 100644 (file)
@@ -6,14 +6,12 @@
 use std::path::Path;
 
 pub struct DynamicLibrary {
-    handle: *mut u8
+    handle: *mut u8,
 }
 
 impl Drop for DynamicLibrary {
     fn drop(&mut self) {
-        unsafe {
-            dl::close(self.handle)
-        }
+        unsafe { dl::close(self.handle) }
     }
 }
 
@@ -28,31 +26,7 @@ pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
         // run.
         match maybe_library {
             Err(err) => Err(err),
-            Ok(handle) => Ok(DynamicLibrary { handle })
-        }
-    }
-
-    /// Loads a dynamic library into the global namespace (RTLD_GLOBAL on Unix)
-    /// and do it now (don't use RTLD_LAZY on Unix).
-    pub fn open_global_now(filename: &Path) -> Result<DynamicLibrary, String> {
-        let maybe_library = dl::open_global_now(filename.as_os_str());
-        match maybe_library {
-            Err(err) => Err(err),
-            Ok(handle) => Ok(DynamicLibrary { handle })
-        }
-    }
-
-    /// Returns the environment variable for this process's dynamic library
-    /// search path
-    pub fn envvar() -> &'static str {
-        if cfg!(windows) {
-            "PATH"
-        } else if cfg!(target_os = "macos") {
-            "DYLD_LIBRARY_PATH"
-        } else if cfg!(target_os = "haiku") {
-            "LIBRARY_PATH"
-        } else {
-            "LD_LIBRARY_PATH"
+            Ok(handle) => Ok(DynamicLibrary { handle }),
         }
     }
 
@@ -68,7 +42,7 @@ pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<*mut T, String> {
         // the destructor does not run.
         match maybe_symbol_value {
             Err(err) => Err(err),
-            Ok(symbol_value) => Ok(symbol_value as *mut T)
+            Ok(symbol_value) => Ok(symbol_value as *mut T),
         }
     }
 }
@@ -78,26 +52,17 @@ pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<*mut T, String> {
 
 #[cfg(unix)]
 mod dl {
-    use std::ffi::{CStr, OsStr, CString};
+    use std::ffi::{CStr, CString, OsStr};
     use std::os::unix::prelude::*;
     use std::ptr;
     use std::str;
 
-    pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
-        check_for_errors_in(|| {
-            unsafe {
-                match filename {
-                    Some(filename) => open_external(filename),
-                    None => open_internal(),
-                }
-            }
-        })
-    }
-
-    pub fn open_global_now(filename: &OsStr) -> Result<*mut u8, String> {
+    pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
         check_for_errors_in(|| unsafe {
-            let s = CString::new(filename.as_bytes()).unwrap();
-            libc::dlopen(s.as_ptr(), libc::RTLD_GLOBAL | libc::RTLD_NOW) as *mut u8
+            match filename {
+                Some(filename) => open_external(filename),
+                None => open_internal(),
+            }
         })
     }
 
@@ -110,7 +75,8 @@ unsafe fn open_internal() -> *mut u8 {
         libc::dlopen(ptr::null(), libc::RTLD_LAZY) as *mut u8
     }
 
-    pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
+    fn check_for_errors_in<T, F>(f: F) -> Result<T, String>
+    where
         F: FnOnce() -> T,
     {
         use std::sync::{Mutex, Once};
@@ -139,15 +105,15 @@ pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
         }
     }
 
-    pub unsafe fn symbol(handle: *mut u8,
-                         symbol: *const libc::c_char)
-                         -> Result<*mut u8, String> {
-        check_for_errors_in(|| {
-            libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8
-        })
+    pub(super) unsafe fn symbol(
+        handle: *mut u8,
+        symbol: *const libc::c_char,
+    ) -> Result<*mut u8, String> {
+        check_for_errors_in(|| libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8)
     }
-    pub unsafe fn close(handle: *mut u8) {
-        libc::dlclose(handle as *mut libc::c_void); ()
+    pub(super) unsafe fn close(handle: *mut u8) {
+        libc::dlclose(handle as *mut libc::c_void);
+        ()
     }
 }
 
@@ -158,7 +124,7 @@ mod dl {
     use std::os::windows::prelude::*;
     use std::ptr;
 
-    use libc::{c_uint, c_void, c_char};
+    use libc::{c_char, c_uint, c_void};
 
     type DWORD = u32;
     type HMODULE = *mut u8;
@@ -167,49 +133,35 @@ mod dl {
     type LPCSTR = *const i8;
 
     extern "system" {
-        fn SetThreadErrorMode(dwNewMode: DWORD,
-                              lpOldMode: *mut DWORD) -> c_uint;
+        fn SetThreadErrorMode(dwNewMode: DWORD, lpOldMode: *mut DWORD) -> c_uint;
         fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
-        fn GetModuleHandleExW(dwFlags: DWORD,
-                              name: LPCWSTR,
-                              handle: *mut HMODULE) -> BOOL;
-        fn GetProcAddress(handle: HMODULE,
-                          name: LPCSTR) -> *mut c_void;
+        fn GetModuleHandleExW(dwFlags: DWORD, name: LPCWSTR, handle: *mut HMODULE) -> BOOL;
+        fn GetProcAddress(handle: HMODULE, name: LPCSTR) -> *mut c_void;
         fn FreeLibrary(handle: HMODULE) -> BOOL;
     }
 
-    pub fn open_global_now(filename: &OsStr) -> Result<*mut u8, String> {
-        open(Some(filename))
-    }
-
-    pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
+    pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
         // disable "dll load failed" error dialog.
         let prev_error_mode = unsafe {
             // SEM_FAILCRITICALERRORS 0x01
             let new_error_mode = 1;
             let mut prev_error_mode = 0;
-            let result = SetThreadErrorMode(new_error_mode,
-                                            &mut prev_error_mode);
+            let result = SetThreadErrorMode(new_error_mode, &mut prev_error_mode);
             if result == 0 {
-                return Err(io::Error::last_os_error().to_string())
+                return Err(io::Error::last_os_error().to_string());
             }
             prev_error_mode
         };
 
         let result = match filename {
             Some(filename) => {
-                let filename_str: Vec<_> =
-                    filename.encode_wide().chain(Some(0)).collect();
-                let result = unsafe {
-                    LoadLibraryW(filename_str.as_ptr())
-                };
+                let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
+                let result = unsafe { LoadLibraryW(filename_str.as_ptr()) };
                 ptr_result(result)
             }
             None => {
                 let mut handle = ptr::null_mut();
-                let succeeded = unsafe {
-                    GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle)
-                };
+                let succeeded = unsafe { GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle) };
                 if succeeded == 0 {
                     Err(io::Error::last_os_error().to_string())
                 } else {
@@ -225,22 +177,16 @@ pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
         result
     }
 
-    pub unsafe fn symbol(handle: *mut u8,
-                         symbol: *const c_char)
-                         -> Result<*mut u8, String> {
+    pub(super) unsafe fn symbol(handle: *mut u8, symbol: *const c_char) -> Result<*mut u8, String> {
         let ptr = GetProcAddress(handle as HMODULE, symbol) as *mut u8;
         ptr_result(ptr)
     }
 
-    pub unsafe fn close(handle: *mut u8) {
+    pub(super) unsafe fn close(handle: *mut u8) {
         FreeLibrary(handle as HMODULE);
     }
 
     fn ptr_result<T>(ptr: *mut T) -> Result<*mut T, String> {
-        if ptr.is_null() {
-            Err(io::Error::last_os_error().to_string())
-        } else {
-            Ok(ptr)
-        }
+        if ptr.is_null() { Err(io::Error::last_os_error().to_string()) } else { Ok(ptr) }
     }
 }