diff options
| author | bors <bors@rust-lang.org> | 2018-04-24 01:15:36 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-04-24 01:15:36 +0000 |
| commit | a1286f6835ade2d46b936100acd82d44093b3b68 (patch) | |
| tree | 82dd0fe4ec36797dc742c20aebce18889332720d /src/libstd | |
| parent | 725c9b0ede0b222cd64a8dc87cbeed052ecad157 (diff) | |
| parent | 7cbc93b14f0c5b39003a95f6cfbb74bd4fa04f40 (diff) | |
| download | rust-a1286f6835ade2d46b936100acd82d44093b3b68.tar.gz rust-a1286f6835ade2d46b936100acd82d44093b3b68.zip | |
Auto merge of #48989 - ExpHP:path-prefix, r=dtolnay
Make signature of Path::strip_prefix accept non-references I did this a while back but didn't submit a PR. Might as well see what happens. Fixes #48390. **Note: This has the potential to cause regressions in type inference.** However, in order for code to break, it would need to be relying on the signature to determine that a type is `&_`, while still being able to figure out what the `_` is. I'm having a hard time imagining such a scenario in real code.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index ec961575473..955a6af1ae6 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -298,10 +298,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<A, I, J>(mut iter: I, mut prefix: J) -> Option<I> - where I: Iterator<Item = A> + Clone, - J: Iterator<Item = A>, - A: PartialEq +fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I> + where I: Iterator<Item = Component<'a>> + Clone, + J: Iterator<Item = Component<'b>>, { loop { let mut iter_next = iter.clone(); @@ -1967,7 +1966,7 @@ impl Path { /// # Examples /// /// ``` - /// use std::path::Path; + /// use std::path::{Path, PathBuf}; /// /// let path = Path::new("/test/haha/foo.txt"); /// @@ -1978,17 +1977,20 @@ 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<P>(&self, base: P) + -> Result<&Path, StripPrefixError> where P: AsRef<Path> { self._strip_prefix(base.as_ref()) } - fn _strip_prefix<'a>(&'a self, base: &'a 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(())) |
