Mercurial > public > mercurial-scm > hg
comparison rust/hgcli/src/main.rs @ 35632:fa9747e7fc86
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.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 12 Jan 2018 22:18:42 +0900 |
parents | edbe11cfedcf |
children | 5c9c71cde1c9 |
comparison
equal
deleted
inserted
replaced
35631:edbe11cfedcf | 35632:fa9747e7fc86 |
---|---|
14 | 14 |
15 use std::env; | 15 use std::env; |
16 use std::path::PathBuf; | 16 use std::path::PathBuf; |
17 use std::ffi::{CString, OsStr}; | 17 use std::ffi::{CString, OsStr}; |
18 #[cfg(target_family = "unix")] | 18 #[cfg(target_family = "unix")] |
19 use std::os::unix::ffi::OsStringExt; | 19 use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
20 | 20 |
21 #[derive(Debug)] | 21 #[derive(Debug)] |
22 struct Environment { | 22 struct Environment { |
23 _exe: PathBuf, | 23 _exe: PathBuf, |
24 python_exe: PathBuf, | 24 python_exe: PathBuf, |
60 python_home: python_home, | 60 python_home: python_home, |
61 mercurial_modules: mercurial_modules.to_path_buf(), | 61 mercurial_modules: mercurial_modules.to_path_buf(), |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 // On UNIX, platform string is just bytes and should not contain NUL. | |
66 #[cfg(target_family = "unix")] | |
67 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString { | |
68 CString::new(s.as_ref().as_bytes()).unwrap() | |
69 } | |
70 | |
71 // TODO convert to ANSI characters? | |
72 #[cfg(target_family = "windows")] | |
65 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString { | 73 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString { |
66 CString::new(s.as_ref().to_str().unwrap()).unwrap() | 74 CString::new(s.as_ref().to_str().unwrap()).unwrap() |
67 } | 75 } |
68 | 76 |
69 // On UNIX, argv starts as an array of char*. So it is easy to convert | 77 // On UNIX, argv starts as an array of char*. So it is easy to convert |