@@ -16,10 +16,9 @@ impl Drop for DynamicLibrary {
16
16
}
17
17
18
18
impl DynamicLibrary {
19
- /// Lazily open a dynamic library. When passed None it gives a
20
- /// handle to the calling process
21
- pub fn open ( filename : Option < & Path > ) -> Result < DynamicLibrary , String > {
22
- let maybe_library = dl:: open ( filename. map ( |path| path. as_os_str ( ) ) ) ;
19
+ /// Lazily open a dynamic library.
20
+ pub fn open ( filename : & Path ) -> Result < DynamicLibrary , String > {
21
+ let maybe_library = dl:: open ( filename. as_os_str ( ) ) ;
23
22
24
23
// The dynamic library must not be constructed if there is
25
24
// an error opening the library so the destructor does not
@@ -57,24 +56,13 @@ mod dl {
57
56
use std:: ptr;
58
57
use std:: str;
59
58
60
- pub ( super ) fn open ( filename : Option < & OsStr > ) -> Result < * mut u8 , String > {
59
+ pub ( super ) fn open ( filename : & OsStr ) -> Result < * mut u8 , String > {
61
60
check_for_errors_in ( || unsafe {
62
- match filename {
63
- Some ( filename) => open_external ( filename) ,
64
- None => open_internal ( ) ,
65
- }
61
+ let s = CString :: new ( filename. as_bytes ( ) ) . unwrap ( ) ;
62
+ libc:: dlopen ( s. as_ptr ( ) , libc:: RTLD_LAZY ) as * mut u8
66
63
} )
67
64
}
68
65
69
- unsafe fn open_external ( filename : & OsStr ) -> * mut u8 {
70
- let s = CString :: new ( filename. as_bytes ( ) ) . unwrap ( ) ;
71
- libc:: dlopen ( s. as_ptr ( ) , libc:: RTLD_LAZY ) as * mut u8
72
- }
73
-
74
- unsafe fn open_internal ( ) -> * mut u8 {
75
- libc:: dlopen ( ptr:: null ( ) , libc:: RTLD_LAZY ) as * mut u8
76
- }
77
-
78
66
fn check_for_errors_in < T , F > ( f : F ) -> Result < T , String >
79
67
where
80
68
F : FnOnce ( ) -> T ,
@@ -124,10 +112,10 @@ mod dl {
124
112
125
113
use winapi:: shared:: minwindef:: HMODULE ;
126
114
use winapi:: um:: errhandlingapi:: SetThreadErrorMode ;
127
- use winapi:: um:: libloaderapi:: { FreeLibrary , GetModuleHandleExW , GetProcAddress , LoadLibraryW } ;
115
+ use winapi:: um:: libloaderapi:: { FreeLibrary , GetProcAddress , LoadLibraryW } ;
128
116
use winapi:: um:: winbase:: SEM_FAILCRITICALERRORS ;
129
117
130
- pub ( super ) fn open ( filename : Option < & OsStr > ) -> Result < * mut u8 , String > {
118
+ pub ( super ) fn open ( filename : & OsStr ) -> Result < * mut u8 , String > {
131
119
// disable "dll load failed" error dialog.
132
120
let prev_error_mode = unsafe {
133
121
let new_error_mode = SEM_FAILCRITICALERRORS ;
@@ -139,22 +127,9 @@ mod dl {
139
127
prev_error_mode
140
128
} ;
141
129
142
- let result = match filename {
143
- Some ( filename) => {
144
- let filename_str: Vec < _ > = filename. encode_wide ( ) . chain ( Some ( 0 ) ) . collect ( ) ;
145
- let result = unsafe { LoadLibraryW ( filename_str. as_ptr ( ) ) } as * mut u8 ;
146
- ptr_result ( result)
147
- }
148
- None => {
149
- let mut handle = ptr:: null_mut ( ) ;
150
- let succeeded = unsafe { GetModuleHandleExW ( 0 , ptr:: null ( ) , & mut handle) } ;
151
- if succeeded == 0 {
152
- Err ( io:: Error :: last_os_error ( ) . to_string ( ) )
153
- } else {
154
- Ok ( handle as * mut u8 )
155
- }
156
- }
157
- } ;
130
+ let filename_str: Vec < _ > = filename. encode_wide ( ) . chain ( Some ( 0 ) ) . collect ( ) ;
131
+ let result = unsafe { LoadLibraryW ( filename_str. as_ptr ( ) ) } as * mut u8 ;
132
+ let result = ptr_result ( result) ;
158
133
159
134
unsafe {
160
135
SetThreadErrorMode ( prev_error_mode, ptr:: null_mut ( ) ) ;
0 commit comments