From 0d0a470b89df7b859ce048e29873621c957e29d8 Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Tue, 20 Feb 2018 18:53:56 -0500 Subject: [PATCH 1/2] Make signature of Path::strip_prefix un-bizarre BREAKING CHANGE: This has the potential to cause regressions in type inference. --- src/libstd/path.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index e03a182653e..527246fe1cd 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -297,10 +297,9 @@ pub const MAIN_SEPARATOR: char = ::sys::path::MAIN_SEP; // Iterate through `iter` while it matches `prefix`; return `None` if `prefix` // is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving // `iter` after having exhausted `prefix`. -fn iter_after(mut iter: I, mut prefix: J) -> Option - where I: Iterator + Clone, - J: Iterator, - A: PartialEq +fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option + where I: Iterator> + Clone, + J: Iterator>, { loop { let mut iter_next = iter.clone(); @@ -1865,7 +1864,7 @@ impl Path { /// # Examples /// /// ``` - /// use std::path::Path; + /// use std::path::{Path, PathBuf}; /// /// let path = Path::new("/test/haha/foo.txt"); /// @@ -1876,16 +1875,19 @@ impl Path { /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new(""))); /// assert_eq!(path.strip_prefix("test").is_ok(), false); /// assert_eq!(path.strip_prefix("/haha").is_ok(), false); + /// + /// let prefix = PathBuf::from("/test/"); + /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt"))); /// ``` #[stable(since = "1.7.0", feature = "path_strip_prefix")] - pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P) - -> Result<&'a Path, StripPrefixError> + pub fn strip_prefix<'a, P>(&'a self, base: P) + -> Result<&'a Path, StripPrefixError> where P: AsRef { self._strip_prefix(base.as_ref()) } - fn _strip_prefix<'a>(&'a self, base: &'a Path) + fn _strip_prefix<'a>(&'a self, base: &Path) -> Result<&'a Path, StripPrefixError> { iter_after(self.components(), base.components()) .map(|c| c.as_path()) From 7cbc93b14f0c5b39003a95f6cfbb74bd4fa04f40 Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Sat, 17 Mar 2018 20:02:55 -0400 Subject: [PATCH 2/2] elide elidable lifetime in Path::strip_prefix --- src/libstd/path.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 527246fe1cd..c624a0b2a2f 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1880,15 +1880,15 @@ impl Path { /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt"))); /// ``` #[stable(since = "1.7.0", feature = "path_strip_prefix")] - pub fn strip_prefix<'a, P>(&'a self, base: P) - -> Result<&'a Path, StripPrefixError> + pub fn strip_prefix

(&self, base: P) + -> Result<&Path, StripPrefixError> where P: AsRef { self._strip_prefix(base.as_ref()) } - fn _strip_prefix<'a>(&'a self, base: &Path) - -> Result<&'a Path, StripPrefixError> { + fn _strip_prefix(&self, base: &Path) + -> Result<&Path, StripPrefixError> { iter_after(self.components(), base.components()) .map(|c| c.as_path()) .ok_or(StripPrefixError(()))