|
1 | 1 | use std::ffi::OsStr;
|
| 2 | +use std::iter::FusedIterator; |
2 | 3 |
|
3 |
| -use crate::path::{Ancestors, Components, Display, Iter, PathBuf, StripPrefixError}; |
| 4 | +use crate::path::{Components, Display, Iter, PathBuf, StripPrefixError}; |
4 | 5 | use crate::{fs, io};
|
5 | 6 |
|
6 | 7 | /// This struct is an async version of [`std::path::Path`].
|
@@ -35,7 +36,7 @@ impl Path {
|
35 | 36 | /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html
|
36 | 37 | /// [`parent`]: struct.Path.html#method.parent
|
37 | 38 | pub fn ancestors(&self) -> Ancestors<'_> {
|
38 |
| - self.inner.ancestors() |
| 39 | + Ancestors { next: Some(&self) } |
39 | 40 | }
|
40 | 41 |
|
41 | 42 | /// Yields the underlying [`OsStr`] slice.
|
@@ -752,6 +753,42 @@ impl Path {
|
752 | 753 | }
|
753 | 754 | }
|
754 | 755 |
|
| 756 | +/// An iterator over [`Path`] and its ancestors. |
| 757 | +/// |
| 758 | +/// This `struct` is created by the [`ancestors`] method on [`Path`]. |
| 759 | +/// See its documentation for more. |
| 760 | +/// |
| 761 | +/// # Examples |
| 762 | +/// |
| 763 | +/// ``` |
| 764 | +/// use async_std::path::Path; |
| 765 | +/// |
| 766 | +/// let path = Path::new("/foo/bar"); |
| 767 | +/// |
| 768 | +/// for ancestor in path.ancestors() { |
| 769 | +/// println!("{}", ancestor.display()); |
| 770 | +/// } |
| 771 | +/// ``` |
| 772 | +/// |
| 773 | +/// [`ancestors`]: struct.Path.html#method.ancestors |
| 774 | +/// [`Path`]: struct.Path.html |
| 775 | +#[derive(Copy, Clone, Debug)] |
| 776 | +pub struct Ancestors<'a> { |
| 777 | + next: Option<&'a Path>, |
| 778 | +} |
| 779 | + |
| 780 | +impl<'a> Iterator for Ancestors<'a> { |
| 781 | + type Item = &'a Path; |
| 782 | + |
| 783 | + fn next(&mut self) -> Option<Self::Item> { |
| 784 | + let next = self.next; |
| 785 | + self.next = next.and_then(Path::parent); |
| 786 | + next |
| 787 | + } |
| 788 | +} |
| 789 | + |
| 790 | +impl FusedIterator for Ancestors<'_> {} |
| 791 | + |
755 | 792 | impl<'a> From<&'a std::path::Path> for &'a Path {
|
756 | 793 | fn from(path: &'a std::path::Path) -> &'a Path {
|
757 | 794 | &Path::new(path.as_os_str())
|
|
0 commit comments