Skip to content

Commit d7cf592

Browse files
committed
with_native_path
1 parent 0ca4ea0 commit d7cf592

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

library/std/src/path.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1125,12 +1125,17 @@ impl FusedIterator for Ancestors<'_> {}
11251125
pub trait PathLike {
11261126
/// Convert to a `Path` reference.
11271127
fn with_path<T, F: FnOnce(&Path) -> T>(&self, f: F) -> T;
1128+
/// Convert to the native platform representation of a path.
1129+
fn with_native_path<T, F: FnOnce(&crate::sys::path::NativePath) -> io::Result<T>>(&self, f: F) -> io::Result<T>;
11281130
}
11291131
#[unstable(feature = "path_like", issue = "none")]
11301132
impl<P: AsRef<Path>> PathLike for P {
11311133
fn with_path<T, F: FnOnce(&Path) -> T>(&self, f: F) -> T {
11321134
f(self.as_ref())
11331135
}
1136+
fn with_native_path<T, F: FnOnce(&crate::sys::path::NativePath) -> io::Result<T>>(&self, f: F) -> io::Result<T> {
1137+
crate::sys::path::with_native_path(self.as_ref(), f)
1138+
}
11341139
}
11351140

11361141
/// An owned, mutable path (akin to [`String`]).

library/std/src/sys/unix/path.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use crate::env;
2-
use crate::ffi::OsStr;
2+
use crate::ffi::{CStr, OsStr};
33
use crate::io;
44
use crate::path::{Path, PathBuf, Prefix};
5+
use crate::sys::common::small_c_string::run_path_with_cstr;
6+
7+
pub type NativePath = CStr;
8+
pub fn with_native_path<T, F: FnOnce(&NativePath) -> io::Result<T>>(path: &Path, f: F) -> io::Result<T> {
9+
run_path_with_cstr(path, f)
10+
}
511

612
#[inline]
713
pub fn is_sep_byte(b: u8) -> bool {

library/std/src/sys/windows/path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ mod tests;
1111
pub const MAIN_SEP_STR: &str = "\\";
1212
pub const MAIN_SEP: char = '\\';
1313

14+
// Currently a no-op. Should convert to a wide string.
15+
pub type NativePath = Path;
16+
pub fn with_native_path<T, F: FnOnce(&NativePath) -> T>(path: &Path, f: F) -> T {
17+
f(path)
18+
}
19+
1420
/// # Safety
1521
///
1622
/// `bytes` must be a valid wtf8 encoded slice

0 commit comments

Comments
 (0)