Skip to content

Commit b9e9b4a

Browse files
committed
Add std::path::Path::ancestors
Squashed commit of the following: commit 1b5d55e26f667b1a25c83c5db0cbb072013a5122 Author: Tobias Stolzmann <[email protected]> Date: Wed Feb 28 00:06:15 2018 +0100 Bugfix commit 4265c2db0b0aaa66fdeace5d329665fd2d13903a Author: Tobias Stolzmann <[email protected]> Date: Tue Feb 27 22:59:12 2018 +0100 Rename std::path::Path::parents into std::path::Path::ancestors commit 2548e4b14d377d20adad0f08304a0dd6f8e48e23 Author: Tobias Stolzmann <[email protected]> Date: Tue Feb 27 12:50:37 2018 +0100 Add tracking issue commit 3e2ce51a6eea0e39af05849f76dd2cefd5035e86 Author: Tobias Stolzmann <[email protected]> Date: Mon Feb 26 15:05:15 2018 +0100 impl FusedIterator for Parents commit a7e096420809740311e19d963d4aba6df77be2f9 Author: Tobias Stolzmann <[email protected]> Date: Mon Feb 26 14:38:41 2018 +0100 Clarify that the iterator returned will yield at least one value commit 796a36ea203cd197cc4c810eebd21c7e3433e6f1 Author: Tobias Stolzmann <[email protected]> Date: Thu Feb 22 14:01:21 2018 +0100 Fix examples commit e279383b21f11c97269cb355a5b2a0ecdb65bb0c Author: Tobias Stolzmann <[email protected]> Date: Thu Feb 22 04:47:24 2018 +0100 Add std::path::Path::parents
1 parent 0ff9872 commit b9e9b4a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/libstd/path.rs

+75
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,50 @@ impl<'a> cmp::Ord for Components<'a> {
10351035
}
10361036
}
10371037

1038+
/// An iterator over [`Path`] and its ancestors.
1039+
///
1040+
/// This `struct` is created by the [`ancestors`] method on [`Path`].
1041+
/// See its documentation for more.
1042+
///
1043+
/// # Examples
1044+
///
1045+
/// ```
1046+
/// #![feature(path_ancestors)]
1047+
///
1048+
/// use std::path::Path;
1049+
///
1050+
/// let path = Path::new("/foo/bar");
1051+
///
1052+
/// for ancestor in path.ancestors() {
1053+
/// println!("{}", ancestor.display());
1054+
/// }
1055+
/// ```
1056+
///
1057+
/// [`ancestors`]: struct.Path.html#method.ancestors
1058+
/// [`Path`]: struct.Path.html
1059+
#[derive(Copy, Clone, Debug)]
1060+
#[unstable(feature = "path_ancestors", issue = "48581")]
1061+
pub struct Ancestors<'a> {
1062+
next: Option<&'a Path>,
1063+
}
1064+
1065+
#[unstable(feature = "path_ancestors", issue = "48581")]
1066+
impl<'a> Iterator for Ancestors<'a> {
1067+
type Item = &'a Path;
1068+
1069+
fn next(&mut self) -> Option<Self::Item> {
1070+
let next = self.next;
1071+
self.next = match next {
1072+
Some(path) => path.parent(),
1073+
None => None,
1074+
};
1075+
next
1076+
}
1077+
}
1078+
1079+
#[unstable(feature = "fused", issue = "35602")]
1080+
impl<'a> FusedIterator for Ancestors<'a> {}
1081+
10381082
////////////////////////////////////////////////////////////////////////////////
10391083
// Basic types and traits
10401084
////////////////////////////////////////////////////////////////////////////////
@@ -1820,6 +1864,37 @@ impl Path {
18201864
})
18211865
}
18221866

1867+
/// Produces an iterator over `Path` and its ancestors.
1868+
///
1869+
/// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
1870+
/// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
1871+
/// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
1872+
/// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
1873+
/// namely `&self`.
1874+
///
1875+
/// # Examples
1876+
///
1877+
/// ```
1878+
/// #![feature(path_ancestors)]
1879+
///
1880+
/// use std::path::Path;
1881+
///
1882+
/// let mut ancestors = Path::new("/foo/bar").ancestors();
1883+
/// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
1884+
/// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
1885+
/// assert_eq!(ancestors.next(), Some(Path::new("/")));
1886+
/// assert_eq!(ancestors.next(), None);
1887+
/// ```
1888+
///
1889+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
1890+
/// [`parent`]: struct.Path.html#method.parent
1891+
#[unstable(feature = "path_ancestors", issue = "48581")]
1892+
pub fn ancestors(&self) -> Ancestors {
1893+
Ancestors {
1894+
next: Some(&self),
1895+
}
1896+
}
1897+
18231898
/// Returns the final component of the `Path`, if there is one.
18241899
///
18251900
/// If the path is a normal file, this is the file name. If it's the path of a directory, this

0 commit comments

Comments
 (0)