Skip to content

Commit 19ae976

Browse files
authored
Rollup merge of #145242 - joboet:tait-split-paths, r=Mark-Simulacrum
std: use a TAIT to define `SplitPaths` on UNIX Defining `SplitPaths` as a TAIT allows using closures instead of function pointers for `split` and `map`.
2 parents fe55364 + 85cefab commit 19ae976

File tree

1 file changed

+7
-29
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+7
-29
lines changed

library/std/src/sys/pal/unix/os.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use crate::{fmt, io, iter, mem, ptr, slice, str};
1616

1717
const TMPBUF_SZ: usize = 128;
1818

19-
const PATH_SEPARATOR: u8 = cfg_select! {
20-
target_os = "redox" => b';',
21-
_ => b':',
22-
};
19+
const PATH_SEPARATOR: u8 = if cfg!(target_os = "redox") { b';' } else { b':' };
2320

2421
unsafe extern "C" {
2522
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks", target_os = "rtems")))]
@@ -189,33 +186,14 @@ pub fn chdir(p: &path::Path) -> io::Result<()> {
189186
if result == 0 { Ok(()) } else { Err(io::Error::last_os_error()) }
190187
}
191188

192-
pub struct SplitPaths<'a> {
193-
iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, fn(&'a [u8]) -> PathBuf>,
194-
}
189+
pub type SplitPaths<'a> = impl Iterator<Item = PathBuf>;
195190

191+
#[define_opaque(SplitPaths)]
196192
pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
197-
fn bytes_to_path(b: &[u8]) -> PathBuf {
198-
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
199-
}
200-
fn is_separator(b: &u8) -> bool {
201-
*b == PATH_SEPARATOR
202-
}
203-
let unparsed = unparsed.as_bytes();
204-
SplitPaths {
205-
iter: unparsed
206-
.split(is_separator as fn(&u8) -> bool)
207-
.map(bytes_to_path as fn(&[u8]) -> PathBuf),
208-
}
209-
}
210-
211-
impl<'a> Iterator for SplitPaths<'a> {
212-
type Item = PathBuf;
213-
fn next(&mut self) -> Option<PathBuf> {
214-
self.iter.next()
215-
}
216-
fn size_hint(&self) -> (usize, Option<usize>) {
217-
self.iter.size_hint()
218-
}
193+
unparsed
194+
.as_bytes()
195+
.split(|&b| b == PATH_SEPARATOR)
196+
.map(|part| PathBuf::from(OsStr::from_bytes(part)))
219197
}
220198

221199
#[derive(Debug)]

0 commit comments

Comments
 (0)