Skip to content

Commit 759e357

Browse files
committed
Implemented Path::ancestors
1 parent a57ba7e commit 759e357

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/path/path.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::OsStr;
22

3-
use crate::path::{Components, PathBuf};
3+
use crate::path::{Ancestors, Components, PathBuf};
44
use crate::{fs, io};
55

66
/// This struct is an async version of [`std::path::Path`].
@@ -12,6 +12,32 @@ pub struct Path {
1212
}
1313

1414
impl Path {
15+
/// Produces an iterator over `Path` and its ancestors.
16+
///
17+
/// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
18+
/// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
19+
/// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
20+
/// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
21+
/// namely `&self`.
22+
///
23+
/// # Examples
24+
///
25+
/// ```
26+
/// use async_std::path::Path;
27+
///
28+
/// let mut ancestors = Path::new("/foo/bar").ancestors();
29+
/// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar").into()));
30+
/// assert_eq!(ancestors.next(), Some(Path::new("/foo").into()));
31+
/// assert_eq!(ancestors.next(), Some(Path::new("/").into()));
32+
/// assert_eq!(ancestors.next(), None);
33+
/// ```
34+
///
35+
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html
36+
/// [`parent`]: struct.Path.html#method.parent
37+
pub fn ancestors(&self) -> Ancestors<'_> {
38+
self.inner.ancestors()
39+
}
40+
1541
/// Yields the underlying [`OsStr`] slice.
1642
///
1743
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html

0 commit comments

Comments
 (0)