Skip to content

Commit

Permalink
Rollup merge of rust-lang#48420 - teiesti:path_parents, r=BurntSushi
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Feb 28, 2018
2 parents 1251560 + b9e9b4a commit 6b5519d
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,50 @@ impl<'a> cmp::Ord for Components<'a> {
}
}

/// An iterator over [`Path`] and its ancestors.
///
/// This `struct` is created by the [`ancestors`] method on [`Path`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// #![feature(path_ancestors)]
///
/// use 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)]
#[unstable(feature = "path_ancestors", issue = "48581")]
pub struct Ancestors<'a> {
next: Option<&'a Path>,
}

#[unstable(feature = "path_ancestors", issue = "48581")]
impl<'a> Iterator for Ancestors<'a> {
type Item = &'a Path;

fn next(&mut self) -> Option<Self::Item> {
let next = self.next;
self.next = match next {
Some(path) => path.parent(),
None => None,
};
next
}
}

#[unstable(feature = "fused", issue = "35602")]
impl<'a> FusedIterator for Ancestors<'a> {}

////////////////////////////////////////////////////////////////////////////////
// Basic types and traits
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1820,6 +1864,37 @@ impl Path {
})
}

/// Produces an iterator over `Path` and its ancestors.
///
/// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
/// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
/// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
/// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
/// namely `&self`.
///
/// # Examples
///
/// ```
/// #![feature(path_ancestors)]
///
/// use std::path::Path;
///
/// let mut ancestors = Path::new("/foo/bar").ancestors();
/// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
/// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
/// assert_eq!(ancestors.next(), Some(Path::new("/")));
/// assert_eq!(ancestors.next(), None);
/// ```
///
/// [`None`]: ../../std/option/enum.Option.html#variant.None
/// [`parent`]: struct.Path.html#method.parent
#[unstable(feature = "path_ancestors", issue = "48581")]
pub fn ancestors(&self) -> Ancestors {
Ancestors {
next: Some(&self),
}
}

/// Returns the final component of the `Path`, if there is one.
///
/// If the path is a normal file, this is the file name. If it's the path of a directory, this
Expand Down

0 comments on commit 6b5519d

Please sign in to comment.