Skip to content

Commit a1286f6

Browse files
committed
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.
2 parents 725c9b0 + 7cbc93b commit a1286f6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/libstd/path.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ pub const MAIN_SEPARATOR: char = ::sys::path::MAIN_SEP;
298298
// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
299299
// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
300300
// `iter` after having exhausted `prefix`.
301-
fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
302-
where I: Iterator<Item = A> + Clone,
303-
J: Iterator<Item = A>,
304-
A: PartialEq
301+
fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
302+
where I: Iterator<Item = Component<'a>> + Clone,
303+
J: Iterator<Item = Component<'b>>,
305304
{
306305
loop {
307306
let mut iter_next = iter.clone();
@@ -1967,7 +1966,7 @@ impl Path {
19671966
/// # Examples
19681967
///
19691968
/// ```
1970-
/// use std::path::Path;
1969+
/// use std::path::{Path, PathBuf};
19711970
///
19721971
/// let path = Path::new("/test/haha/foo.txt");
19731972
///
@@ -1978,17 +1977,20 @@ impl Path {
19781977
/// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
19791978
/// assert_eq!(path.strip_prefix("test").is_ok(), false);
19801979
/// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
1980+
///
1981+
/// let prefix = PathBuf::from("/test/");
1982+
/// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
19811983
/// ```
19821984
#[stable(since = "1.7.0", feature = "path_strip_prefix")]
1983-
pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
1984-
-> Result<&'a Path, StripPrefixError>
1985+
pub fn strip_prefix<P>(&self, base: P)
1986+
-> Result<&Path, StripPrefixError>
19851987
where P: AsRef<Path>
19861988
{
19871989
self._strip_prefix(base.as_ref())
19881990
}
19891991

1990-
fn _strip_prefix<'a>(&'a self, base: &'a Path)
1991-
-> Result<&'a Path, StripPrefixError> {
1992+
fn _strip_prefix(&self, base: &Path)
1993+
-> Result<&Path, StripPrefixError> {
19921994
iter_after(self.components(), base.components())
19931995
.map(|c| c.as_path())
19941996
.ok_or(StripPrefixError(()))

0 commit comments

Comments
 (0)