about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-06-02 14:51:58 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-06-09 17:46:27 -0700
commitd42cc130f9eaaa4b35944854c3ba34ae98d6361e (patch)
tree02caa183a0848d2533948c9bb8a9fbf7f6b45677 /src/libstd
parent0ea7aa30cc864d00fc30b9ba610f2daefab4e850 (diff)
downloadrust-d42cc130f9eaaa4b35944854c3ba34ae98d6361e.tar.gz
rust-d42cc130f9eaaa4b35944854c3ba34ae98d6361e.zip
std: Remove the as_utf16_p functions
These functions are all much better expressed via RAII using the to_utf16()
method on strings. This refactoring also takes this opportunity to properly
handle when filenames aren't valid unicode when passed through to the windows
I/O layer by properly returning I/O errors.

All previous users of the `as_utf16_p` or `as_utf16_mut_p` functions will need
to convert their code to using `foo.to_utf16().append_one(0)` to get a
null-terminated utf16 string.

[breaking-change]
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/os.rs46
-rw-r--r--src/libstd/unstable/dynamic_lib.rs10
2 files changed, 20 insertions, 36 deletions
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index 90df18106f0..fa882e7d016 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -133,7 +133,7 @@ pub mod win32 {
     use os::TMPBUF_SZ;
     use slice::{MutableVector, ImmutableVector};
     use string::String;
-    use str::{StrSlice, StrAllocating};
+    use str::StrSlice;
     use str;
     use vec::Vec;
 
@@ -171,17 +171,6 @@ pub mod win32 {
             return res;
         }
     }
-
-    pub fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
-        as_mut_utf16_p(s, |t| { f(t as *u16) })
-    }
-
-    pub fn as_mut_utf16_p<T>(s: &str, f: |*mut u16| -> T) -> T {
-        let mut t = s.to_utf16();
-        // Null terminate before passing on.
-        t.push(0u16);
-        f(t.as_mut_ptr())
-    }
 }
 
 /*
@@ -356,11 +345,10 @@ pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
 pub fn getenv(n: &str) -> Option<String> {
     unsafe {
         with_env_lock(|| {
-            use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
-            as_utf16_p(n, |u| {
-                fill_utf16_buf_and_decode(|buf, sz| {
-                    libc::GetEnvironmentVariableW(u, buf, sz)
-                })
+            use os::win32::{fill_utf16_buf_and_decode};
+            let n = n.to_utf16().append_one(0);
+            fill_utf16_buf_and_decode(|buf, sz| {
+                libc::GetEnvironmentVariableW(n.as_ptr(), buf, sz)
             })
         })
     }
@@ -398,14 +386,11 @@ pub fn setenv(n: &str, v: &str) {
 /// Sets the environment variable `n` to the value `v` for the currently running
 /// process
 pub fn setenv(n: &str, v: &str) {
+    let n = n.to_utf16().append_one(0);
+    let v = v.to_utf16().append_one(0);
     unsafe {
         with_env_lock(|| {
-            use os::win32::as_utf16_p;
-            as_utf16_p(n, |nbuf| {
-                as_utf16_p(v, |vbuf| {
-                    libc::SetEnvironmentVariableW(nbuf, vbuf);
-                })
-            })
+            libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr());
         })
     }
 }
@@ -428,12 +413,10 @@ pub fn unsetenv(n: &str) {
     }
     #[cfg(windows)]
     fn _unsetenv(n: &str) {
+        let n = n.to_utf16().append_one(0);
         unsafe {
             with_env_lock(|| {
-                use os::win32::as_utf16_p;
-                as_utf16_p(n, |nbuf| {
-                    libc::SetEnvironmentVariableW(nbuf, ptr::null());
-                })
+                libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null());
             })
         }
     }
@@ -732,11 +715,12 @@ pub fn change_dir(p: &Path) -> bool {
 
     #[cfg(windows)]
     fn chdir(p: &Path) -> bool {
+        let p = match p.as_str() {
+            Some(s) => s.to_utf16().append_one(0),
+            None => return false,
+        };
         unsafe {
-            use os::win32::as_utf16_p;
-            return as_utf16_p(p.as_str().unwrap(), |buf| {
-                libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
-            });
+            libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL)
         }
     }
 
diff --git a/src/libstd/unstable/dynamic_lib.rs b/src/libstd/unstable/dynamic_lib.rs
index c05cdc85cc5..6980c5cc0c2 100644
--- a/src/libstd/unstable/dynamic_lib.rs
+++ b/src/libstd/unstable/dynamic_lib.rs
@@ -272,21 +272,21 @@ pub mod dl {
 
 #[cfg(target_os = "win32")]
 pub mod dl {
+    use c_str::ToCStr;
     use libc;
     use os;
     use ptr;
     use result::{Ok, Err, Result};
-    use string::String;
+    use str::StrAllocating;
     use str;
-    use c_str::ToCStr;
+    use string::String;
 
     pub unsafe fn open_external<T: ToCStr>(filename: T) -> *u8 {
         // Windows expects Unicode data
         let filename_cstr = filename.to_c_str();
         let filename_str = str::from_utf8(filename_cstr.as_bytes_no_nul()).unwrap();
-        os::win32::as_utf16_p(filename_str, |raw_name| {
-            LoadLibraryW(raw_name as *libc::c_void) as *u8
-        })
+        let filename_str = filename_str.to_utf16().append_one(0);
+        LoadLibraryW(filename_str.as_ptr() as *libc::c_void) as *u8
     }
 
     pub unsafe fn open_internal() -> *u8 {