use sys::handle::Handle;
use sys::{c, cvt};
use sys_common::FromInner;
-use vec::Vec;
+
+use super::to_u16s;
pub struct File { handle: Handle }
}
}
-pub fn to_u16s(s: &Path) -> io::Result<Vec<u16>> {
- let mut maybe_result = s.as_os_str().encode_wide().collect();
- if maybe_result.iter().any(|&u| u == 0) {
- return Err(io::Error::new(io::ErrorKind::InvalidInput, "paths cannot contain NULs"));
- }
- maybe_result.push(0);
- Ok(maybe_result)
-}
-
impl FileAttr {
pub fn size(&self) -> u64 {
((self.data.nFileSizeHigh as u64) << 32) | (self.data.nFileSizeLow as u64)
use ffi::OsStr;
use env;
use rand::{self, StdRng, Rng};
+ use vec::Vec;
macro_rules! t {
($e:expr) => (match $e {
}
}
-fn to_utf16_os(s: &OsStr) -> Vec<u16> {
- let mut v: Vec<_> = s.encode_wide().collect();
- v.push(0);
- v
+pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
+ fn inner(s: &OsStr) -> io::Result<Vec<u16>> {
+ let mut maybe_result: Vec<u16> = s.encode_wide().collect();
+ if maybe_result.iter().any(|&u| u == 0) {
+ return Err(io::Error::new(io::ErrorKind::InvalidInput,
+ "strings passed to WinAPI cannot contain NULs"));
+ }
+ maybe_result.push(0);
+ Ok(maybe_result)
+ }
+ inner(s.as_ref())
}
// Many Windows APIs follow a pattern of where we hand a buffer and then they
use sys::{c, cvt};
use sys::handle::Handle;
+use super::to_u16s;
+
pub fn errno() -> i32 {
unsafe { c::GetLastError() as i32 }
}
}
pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
- let k = super::to_utf16_os(k);
+ let k = try!(to_u16s(k));
let res = super::fill_utf16_buf(|buf, sz| unsafe {
c::GetEnvironmentVariableW(k.as_ptr(), buf, sz)
}, |buf| {
}
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
- let k = super::to_utf16_os(k);
- let v = super::to_utf16_os(v);
+ let k = try!(to_u16s(k));
+ let v = try!(to_u16s(v));
cvt(unsafe {
c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr())
}
pub fn unsetenv(n: &OsStr) -> io::Result<()> {
- let v = super::to_utf16_os(n);
+ let v = try!(to_u16s(n));
cvt(unsafe {
c::SetEnvironmentVariableW(v.as_ptr(), ptr::null())
}).map(|_| ())