Skip to content

Commit 89d6116

Browse files
authored
Merge pull request #483 from ryan-scott-dev/rscott/pathbuf_fromiter
Add FromIterator and Extend trait implementations for PathBuf
2 parents e9c0f8f + 8f33660 commit 89d6116

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/path/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
//! path.push("system32");
5353
//!
5454
//! path.set_extension("dll");
55+
//!
56+
//! // ... but push is best used if you don't know everything up
57+
//! // front. If you do, this way is better:
58+
//! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
5559
//! ```
5660
//!
5761
//! [`Component`]: enum.Component.html

src/path/pathbuf.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,17 @@ impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf {
283283
})
284284
}
285285
}
286+
287+
impl<P: AsRef<Path>> std::iter::FromIterator<P> for PathBuf {
288+
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
289+
let mut buf = PathBuf::new();
290+
buf.extend(iter);
291+
buf
292+
}
293+
}
294+
295+
impl<P: AsRef<Path>> std::iter::Extend<P> for PathBuf {
296+
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
297+
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
298+
}
299+
}

0 commit comments

Comments
 (0)