@@ -45,12 +45,22 @@ impl Drop for DynamicLibrary {
45
45
}
46
46
47
47
impl DynamicLibrary {
48
+ // FIXME (#12938): Until DST lands, we cannot decompose &str into
49
+ // & and str, so we cannot usefully take ToCStr arguments by
50
+ // reference (without forcing an additional & around &str). So we
51
+ // are instead temporarily adding an instance for &Path, so that
52
+ // we can take ToCStr as owned. When DST lands, the &Path instance
53
+ // should be removed, and arguments bound by ToCStr should be
54
+ // passed by reference. (Here: in the `open` method.)
55
+
48
56
/// Lazily open a dynamic library. When passed None it gives a
49
57
/// handle to the calling process
50
- pub fn open ( filename : Option < & path:: Path > ) -> Result < DynamicLibrary , ~str > {
58
+ pub fn open < T : ToCStr > ( filename : Option < T > )
59
+ -> Result < DynamicLibrary , ~str > {
51
60
unsafe {
61
+ let mut filename = filename;
52
62
let maybe_library = dl:: check_for_errors_in ( || {
53
- match filename {
63
+ match filename. take ( ) {
54
64
Some ( name) => dl:: open_external ( name) ,
55
65
None => dl:: open_internal ( )
56
66
}
@@ -114,7 +124,8 @@ mod test {
114
124
fn test_loading_cosine ( ) {
115
125
// The math library does not need to be loaded since it is already
116
126
// statically linked in
117
- let libm = match DynamicLibrary :: open ( None ) {
127
+ let none: Option < Path > = None ; // appease the typechecker
128
+ let libm = match DynamicLibrary :: open ( none) {
118
129
Err ( error) => fail ! ( "Could not load self as module: {}" , error) ,
119
130
Ok ( libm) => libm
120
131
} ;
@@ -142,7 +153,7 @@ mod test {
142
153
fn test_errors_do_not_crash ( ) {
143
154
// Open /dev/null as a library to get an error, and make sure
144
155
// that only causes an error, and not a crash.
145
- let path = GenericPath :: new ( "/dev/null" ) ;
156
+ let path = Path :: new ( "/dev/null" ) ;
146
157
match DynamicLibrary :: open ( Some ( & path) ) {
147
158
Err ( _) => { }
148
159
Ok ( _) => fail ! ( "Successfully opened the empty library." )
@@ -157,12 +168,11 @@ mod test {
157
168
pub mod dl {
158
169
use c_str:: ToCStr ;
159
170
use libc;
160
- use path;
161
171
use ptr;
162
172
use str;
163
173
use result:: * ;
164
174
165
- pub unsafe fn open_external ( filename : & path :: Path ) -> * u8 {
175
+ pub unsafe fn open_external < T : ToCStr > ( filename : T ) -> * u8 {
166
176
filename. with_c_str ( |raw_name| {
167
177
dlopen ( raw_name, Lazy as libc:: c_int ) as * u8
168
178
} )
@@ -223,9 +233,14 @@ pub mod dl {
223
233
use os;
224
234
use ptr;
225
235
use result:: { Ok , Err , Result } ;
236
+ use str;
237
+ use c_str:: ToCStr ;
226
238
227
- pub unsafe fn open_external ( filename : & path:: Path ) -> * u8 {
228
- os:: win32:: as_utf16_p ( filename. as_str ( ) . unwrap ( ) , |raw_name| {
239
+ pub unsafe fn open_external < T : ToCStr > ( filename : T ) -> * u8 {
240
+ // Windows expects Unicode data
241
+ let filename_cstr = filename. to_c_str ( ) ;
242
+ let filename_str = str:: from_utf8 ( filename_cstr. as_bytes_no_nul ( ) ) . unwrap ( ) ;
243
+ os:: win32:: as_utf16_p ( filename_str, |raw_name| {
229
244
LoadLibraryW ( raw_name as * libc:: c_void ) as * u8
230
245
} )
231
246
}
0 commit comments