standard_lib/fs/path/
iter.rs1use std::ffi::OsStr;
2use std::marker::PhantomData;
3use std::os::unix::ffi::OsStrExt;
4
5use crate::fs::path::{Path, PathState, Rel};
6
7pub struct Components<'a, State: PathState> {
10 pub(crate) _state: PhantomData<fn() -> State>,
11 pub(crate) path: &'a [u8],
12 pub(crate) head: usize,
13}
14
15impl<'a, S: PathState> Iterator for Components<'a, S> {
16 type Item = &'a Path<Rel>;
17
18 fn next(&mut self) -> Option<Self::Item> {
19 if self.head >= self.path.len() {
20 None?
21 }
22 let mut tail = self.head + 1;
23
24 while let Some(ch) = self.path.get(tail) && *ch != b'/' {
25 tail += 1;
26 }
27
28 let res = &self.path[self.head..tail];
29 self.head = tail;
30
31 unsafe {
32 Some(Path::from_unchecked(OsStr::from_bytes(res)))
33 }
34 }
35}
36
37pub struct Ancestors<'a, State: PathState + 'a> {
38 pub(crate) _state: PhantomData<fn() -> State>,
39 pub(crate) path: &'a [u8],
40 pub(crate) index: usize,
41}
42
43impl<'a, S: PathState> Iterator for Ancestors<'a, S> {
44 type Item = &'a Path<S>;
45
46 fn next(&mut self) -> Option<Self::Item> {
47 if self.index >= self.path.len() {
48 None?
49 }
50 self.index += 1;
51
52 while let Some(ch) = self.path.get(self.index) && *ch != b'/' {
53 self.index += 1;
54 }
55
56 unsafe {
57 Some(Path::from_unchecked(OsStr::from_bytes(
58 &self.path[..self.index]
59 )))
60 }
61 }
62}