1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::iter::FusedIterator;

use crate::path::Path;

/// An iterator over [`Path`] and its ancestors.

///

/// This `struct` is created by the [`ancestors`] method on [`Path`].

/// See its documentation for more.

///

/// # Examples

///

/// ```

/// use async_std::path::Path;

///

/// let path = Path::new("/foo/bar");

///

/// for ancestor in path.ancestors() {

///     println!("{}", ancestor.display());

/// }

/// ```

///

/// [`ancestors`]: struct.Path.html#method.ancestors

/// [`Path`]: struct.Path.html

#[derive(Copy, Clone, Debug)]
pub struct Ancestors<'a> {
    pub(crate) next: Option<&'a Path>,
}

impl<'a> Iterator for Ancestors<'a> {
    type Item = &'a Path;

    fn next(&mut self) -> Option<Self::Item> {
        let next = self.next;
        self.next = next.and_then(Path::parent);
        next
    }
}

impl FusedIterator for Ancestors<'_> {}