| 1 | use std::iter::FusedIterator; |
| 2 | |
| 3 | use crate::path::Path; |
| 4 | |
| 5 | /// An iterator over [`Path`] and its ancestors. |
| 6 | /// |
| 7 | /// This `struct` is created by the [`ancestors`] method on [`Path`]. |
| 8 | /// See its documentation for more. |
| 9 | /// |
| 10 | /// # Examples |
| 11 | /// |
| 12 | /// ``` |
| 13 | /// use async_std::path::Path; |
| 14 | /// |
| 15 | /// let path = Path::new("/foo/bar" ); |
| 16 | /// |
| 17 | /// for ancestor in path.ancestors() { |
| 18 | /// println!("{}" , ancestor.display()); |
| 19 | /// } |
| 20 | /// ``` |
| 21 | /// |
| 22 | /// [`ancestors`]: struct.Path.html#method.ancestors |
| 23 | /// [`Path`]: struct.Path.html |
| 24 | #[derive (Copy, Clone, Debug)] |
| 25 | pub struct Ancestors<'a> { |
| 26 | pub(crate) next: Option<&'a Path>, |
| 27 | } |
| 28 | |
| 29 | impl<'a> Iterator for Ancestors<'a> { |
| 30 | type Item = &'a Path; |
| 31 | |
| 32 | fn next(&mut self) -> Option<Self::Item> { |
| 33 | let next: Option<&'a Path> = self.next; |
| 34 | self.next = next.and_then(Path::parent); |
| 35 | next |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl FusedIterator for Ancestors<'_> {} |
| 40 | |