Skip to content

Commit aa13ba7

Browse files
author
Stjepan Glavina
committed
Refactor
1 parent 5c1e052 commit aa13ba7

File tree

4 files changed

+494
-499
lines changed

4 files changed

+494
-499
lines changed

src/path/ancestors.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 = self.next;
34+
self.next = next.and_then(Path::parent);
35+
next
36+
}
37+
}
38+
39+
impl FusedIterator for Ancestors<'_> {}

src/path/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html
66
7+
mod ancestors;
78
mod path;
89
mod pathbuf;
910

@@ -23,5 +24,6 @@ pub use std::path::MAIN_SEPARATOR;
2324
#[doc(inline)]
2425
pub use std::path::is_separator;
2526

26-
pub use path::{Ancestors, Path};
27+
use ancestors::Ancestors;
28+
pub use path::Path;
2729
pub use pathbuf::PathBuf;

0 commit comments

Comments
 (0)