From 8f3366072f615d14030ba997b3fa30612e5d3044 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Fri, 8 Nov 2019 22:08:53 +1100 Subject: [PATCH] Add FromIterator and Extend trait implementations for PathBuf --- src/path/mod.rs | 4 ++++ src/path/pathbuf.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/path/mod.rs b/src/path/mod.rs index e9843d75..059e6050 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -52,6 +52,10 @@ //! path.push("system32"); //! //! path.set_extension("dll"); +//! +//! // ... but push is best used if you don't know everything up +//! // front. If you do, this way is better: +//! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect(); //! ``` //! //! [`Component`]: enum.Component.html diff --git a/src/path/pathbuf.rs b/src/path/pathbuf.rs index 12f5ac39..a1901115 100644 --- a/src/path/pathbuf.rs +++ b/src/path/pathbuf.rs @@ -280,3 +280,17 @@ impl<'b, P: AsRef + 'b> FromStream

for PathBuf { }) } } + +impl> std::iter::FromIterator

for PathBuf { + fn from_iter>(iter: I) -> PathBuf { + let mut buf = PathBuf::new(); + buf.extend(iter); + buf + } +} + +impl> std::iter::Extend

for PathBuf { + fn extend>(&mut self, iter: I) { + iter.into_iter().for_each(move |p| self.push(p.as_ref())); + } +}