standard_lib/fs/path/
iter.rs1use std::{ffi::OsStr, iter::FusedIterator};
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
37impl<'a, S: PathState> FusedIterator for Components<'a, S> {}
38
39pub struct Ancestors<'a, State: PathState + 'a> {
40 pub(crate) _state: PhantomData<fn() -> State>,
41 pub(crate) path: &'a [u8],
42 pub(crate) index: usize,
43}
44
45impl<'a, S: PathState> Iterator for Ancestors<'a, S> {
46 type Item = &'a Path<S>;
47
48 fn next(&mut self) -> Option<Self::Item> {
49 if self.index >= self.path.len() {
50 None?
51 }
52 self.index += 1;
53
54 while let Some(ch) = self.path.get(self.index) && *ch != b'/' {
55 self.index += 1;
56 }
57
58 unsafe {
59 Some(Path::from_unchecked(OsStr::from_bytes(
60 &self.path[..self.index]
61 )))
62 }
63 }
64}
65
66impl<'a, S: PathState> FusedIterator for Ancestors<'a, S> {}