Skip to content

Commit a367365

Browse files
committed
core: convert Pattern<'a> into Pattern<H: Haystack>
Add a Haystack trait describing something that can be searched in and make core::str::Pattern (and related types) generic on that trait. This will allow Pattern to be used for types other than str (most notably OsStr). This somewhat follows the Pattern API 2.0 design. While that design is apparently abandoned (?), it is somewhat helpful when going for patterns on OsStr, so I’m going with it unless someone tells me otherwise. ;) For now leave Pattern, Haystack et al in core::str::pattern. Since they are no longer str-specific, I’ll move them to core::pattern in future commit. This one leaves them in place to make the diff smaller.
1 parent 4a18324 commit a367365

File tree

9 files changed

+251
-170
lines changed

9 files changed

+251
-170
lines changed

library/alloc/src/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl str {
268268
without modifying the original"]
269269
#[stable(feature = "rust1", since = "1.0.0")]
270270
#[inline]
271-
pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
271+
pub fn replace<'a, P: Pattern<&'a str>>(&'a self, from: P, to: &str) -> String {
272272
let mut result = String::new();
273273
let mut last_end = 0;
274274
for (start, part) in self.match_indices(from) {
@@ -308,7 +308,7 @@ impl str {
308308
#[must_use = "this returns the replaced string as a new allocation, \
309309
without modifying the original"]
310310
#[stable(feature = "str_replacen", since = "1.16.0")]
311-
pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
311+
pub fn replacen<'a, P: Pattern<&'a str>>(&'a self, pat: P, to: &str, count: usize) -> String {
312312
// Hope to reduce the times of re-allocation
313313
let mut result = String::with_capacity(32);
314314
let mut last_end = 0;

library/alloc/src/string.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ impl String {
13711371
#[unstable(feature = "string_remove_matches", reason = "new API", issue = "72826")]
13721372
pub fn remove_matches<'a, P>(&'a mut self, pat: P)
13731373
where
1374-
P: for<'x> Pattern<'x>,
1374+
P: for<'x> Pattern<&'x str>,
13751375
{
13761376
use core::str::pattern::Searcher;
13771377

@@ -2174,10 +2174,10 @@ impl<'a> Extend<Cow<'a, str>> for String {
21742174
reason = "API not fully fleshed out and ready to be stabilized",
21752175
issue = "27721"
21762176
)]
2177-
impl<'a, 'b> Pattern<'a> for &'b String {
2178-
type Searcher = <&'b str as Pattern<'a>>::Searcher;
2177+
impl<'a, 'b> Pattern<&'a str> for &'b String {
2178+
type Searcher = <&'b str as Pattern<&'a str>>::Searcher;
21792179

2180-
fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
2180+
fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<&'a str>>::Searcher {
21812181
self[..].into_searcher(haystack)
21822182
}
21832183

library/alloc/tests/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ mod pattern {
18911891

18921892
fn cmp_search_to_vec<'a>(
18931893
rev: bool,
1894-
pat: impl Pattern<'a, Searcher: ReverseSearcher<'a>>,
1894+
pat: impl Pattern<&'a str, Searcher: ReverseSearcher<&'a str>>,
18951895
haystack: &'a str,
18961896
right: Vec<SearchStep>,
18971897
) {
@@ -2155,7 +2155,7 @@ fn different_str_pattern_forwarding_lifetimes() {
21552155

21562156
fn foo<'a, P>(p: P)
21572157
where
2158-
for<'b> &'b P: Pattern<'a>,
2158+
for<'b> &'b P: Pattern<&'a str>,
21592159
{
21602160
for _ in 0..3 {
21612161
"asdf".find(&p);

0 commit comments

Comments
 (0)