1
1
use std:: ffi:: OsStr ;
2
2
3
- use crate :: path:: { Components , PathBuf } ;
3
+ use crate :: path:: { Ancestors , Components , PathBuf } ;
4
4
use crate :: { fs, io} ;
5
5
6
6
/// This struct is an async version of [`std::path::Path`].
@@ -12,6 +12,32 @@ pub struct Path {
12
12
}
13
13
14
14
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
+
15
41
/// Yields the underlying [`OsStr`] slice.
16
42
///
17
43
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
0 commit comments