Skip to content

Commit

Permalink
Better path_to_c_string impl
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Sep 26, 2024
1 parent 27df5ca commit a3975a3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions compiler/rustc_fs_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(any(unix, windows, target_os = "wasi"))]
use std::ffi::CString;
use std::path::{absolute, Path, PathBuf};
use std::{fs, io};
Expand Down Expand Up @@ -77,14 +76,18 @@ pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<Li
}
}

#[cfg(unix)]
#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
pub fn path_to_c_string(p: &Path) -> CString {
use std::ffi::OsStr;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(all(target_os = "wasi", target_env = "p1"))]
use std::os::wasi::ffi::OsStrExt;

let p: &OsStr = p.as_ref();
CString::new(p.as_bytes()).unwrap()
}
#[cfg(any(windows, target_os = "wasi"))]
#[cfg(windows)]
pub fn path_to_c_string(p: &Path) -> CString {
CString::new(p.to_str().unwrap()).unwrap()
}
Expand Down

0 comments on commit a3975a3

Please sign in to comment.