File tree 3 files changed +20
-18
lines changed
3 files changed +20
-18
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,8 @@ use sync::Arc;
22
22
use sys:: handle:: Handle ;
23
23
use sys:: { c, cvt} ;
24
24
use sys_common:: FromInner ;
25
- use vec:: Vec ;
25
+
26
+ use super :: to_u16s;
26
27
27
28
pub struct File { handle : Handle }
28
29
@@ -377,15 +378,6 @@ impl fmt::Debug for File {
377
378
}
378
379
}
379
380
380
- pub fn to_u16s ( s : & Path ) -> io:: Result < Vec < u16 > > {
381
- let mut maybe_result = s. as_os_str ( ) . encode_wide ( ) . collect ( ) ;
382
- if maybe_result. iter ( ) . any ( |& u| u == 0 ) {
383
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "paths cannot contain NULs" ) ) ;
384
- }
385
- maybe_result. push ( 0 ) ;
386
- Ok ( maybe_result)
387
- }
388
-
389
381
impl FileAttr {
390
382
pub fn size ( & self ) -> u64 {
391
383
( ( self . data . nFileSizeHigh as u64 ) << 32 ) | ( self . data . nFileSizeLow as u64 )
@@ -622,6 +614,7 @@ fn directory_junctions_are_directories() {
622
614
use ffi:: OsStr ;
623
615
use env;
624
616
use rand:: { self , StdRng , Rng } ;
617
+ use vec:: Vec ;
625
618
626
619
macro_rules! t {
627
620
( $e: expr) => ( match $e {
Original file line number Diff line number Diff line change @@ -72,10 +72,17 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
72
72
}
73
73
}
74
74
75
- fn to_utf16_os ( s : & OsStr ) -> Vec < u16 > {
76
- let mut v: Vec < _ > = s. encode_wide ( ) . collect ( ) ;
77
- v. push ( 0 ) ;
78
- v
75
+ pub fn to_u16s < S : AsRef < OsStr > > ( s : S ) -> io:: Result < Vec < u16 > > {
76
+ fn inner ( s : & OsStr ) -> io:: Result < Vec < u16 > > {
77
+ let mut maybe_result: Vec < u16 > = s. encode_wide ( ) . collect ( ) ;
78
+ if maybe_result. iter ( ) . any ( |& u| u == 0 ) {
79
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
80
+ "strings passed to WinAPI cannot contain NULs" ) ) ;
81
+ }
82
+ maybe_result. push ( 0 ) ;
83
+ Ok ( maybe_result)
84
+ }
85
+ inner ( s. as_ref ( ) )
79
86
}
80
87
81
88
// Many Windows APIs follow a pattern of where we hand a buffer and then they
Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ use slice;
28
28
use sys:: { c, cvt} ;
29
29
use sys:: handle:: Handle ;
30
30
31
+ use super :: to_u16s;
32
+
31
33
pub fn errno ( ) -> i32 {
32
34
unsafe { c:: GetLastError ( ) as i32 }
33
35
}
@@ -228,7 +230,7 @@ pub fn chdir(p: &path::Path) -> io::Result<()> {
228
230
}
229
231
230
232
pub fn getenv ( k : & OsStr ) -> io:: Result < Option < OsString > > {
231
- let k = super :: to_utf16_os ( k ) ;
233
+ let k = try! ( to_u16s ( k ) ) ;
232
234
let res = super :: fill_utf16_buf ( |buf, sz| unsafe {
233
235
c:: GetEnvironmentVariableW ( k. as_ptr ( ) , buf, sz)
234
236
} , |buf| {
@@ -247,16 +249,16 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
247
249
}
248
250
249
251
pub fn setenv ( k : & OsStr , v : & OsStr ) -> io:: Result < ( ) > {
250
- let k = super :: to_utf16_os ( k ) ;
251
- let v = super :: to_utf16_os ( v ) ;
252
+ let k = try! ( to_u16s ( k ) ) ;
253
+ let v = try! ( to_u16s ( v ) ) ;
252
254
253
255
cvt ( unsafe {
254
256
c:: SetEnvironmentVariableW ( k. as_ptr ( ) , v. as_ptr ( ) )
255
257
} ) . map ( |_| ( ) )
256
258
}
257
259
258
260
pub fn unsetenv ( n : & OsStr ) -> io:: Result < ( ) > {
259
- let v = super :: to_utf16_os ( n ) ;
261
+ let v = try! ( to_u16s ( n ) ) ;
260
262
cvt ( unsafe {
261
263
c:: SetEnvironmentVariableW ( v. as_ptr ( ) , ptr:: null ( ) )
262
264
} ) . map ( |_| ( ) )
You can’t perform that action at this time.
0 commit comments