rust: convert Unix path to CString transparently
authorYuya Nishihara <yuya@tcha.org>
Fri, 12 Jan 2018 22:18:42 +0900
changeset 35632 fa9747e7fc86
parent 35631 edbe11cfedcf
child 35633 a981ab2a1b4c
rust: convert Unix path to CString transparently On Unix, path is just a sequence of bytes. We shouldn't convert it to UTF-8 string.
rust/hgcli/src/main.rs
--- a/rust/hgcli/src/main.rs	Fri Jan 12 22:09:34 2018 +0900
+++ b/rust/hgcli/src/main.rs	Fri Jan 12 22:18:42 2018 +0900
@@ -16,7 +16,7 @@
 use std::path::PathBuf;
 use std::ffi::{CString, OsStr};
 #[cfg(target_family = "unix")]
-use std::os::unix::ffi::OsStringExt;
+use std::os::unix::ffi::{OsStrExt, OsStringExt};
 
 #[derive(Debug)]
 struct Environment {
@@ -62,6 +62,14 @@
     }
 }
 
+// On UNIX, platform string is just bytes and should not contain NUL.
+#[cfg(target_family = "unix")]
+fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
+    CString::new(s.as_ref().as_bytes()).unwrap()
+}
+
+// TODO convert to ANSI characters?
+#[cfg(target_family = "windows")]
 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
     CString::new(s.as_ref().to_str().unwrap()).unwrap()
 }