Skip to content

Commit 4391e6a

Browse files
committed
core: convert Pattern<'a> into Pattern<H: Haystack> (RFC 2295)
As per RFC 2295, 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). The change mostly follows the RFC though there are some differences in Haystack trait. Most notably, instead of separate StartCursor and EndCursors types, there’s one Cursor type instead. This eliminate the need for methods converting between the two types of cursors. Conversion from cursor to offset isn’t included either since as far as I can tell it’s not needed. A generic code operating on Haystack doesn’t need a concept of offset. It can operate on cursors instead. Lastly, rather than range_to_self as suggested in RFC this commit implements split_at_cursor_unchecked which simplifies default implementation of strip_prefix_of and strip_suffix_of. 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. Issue: rust-lang#49802
1 parent c528357 commit 4391e6a

File tree

11 files changed

+229
-182
lines changed

11 files changed

+229
-182
lines changed

library/alloc/src/str.rs

+2-2
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

+4-4
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ mod pattern {
18791879

18801880
fn cmp_search_to_vec<'a>(
18811881
rev: bool,
1882-
pat: impl Pattern<'a, Searcher: ReverseSearcher<'a>>,
1882+
pat: impl Pattern<&'a str, Searcher: ReverseSearcher<&'a str>>,
18831883
haystack: &'a str,
18841884
right: Vec<SearchStep>,
18851885
) {
@@ -2143,7 +2143,7 @@ fn different_str_pattern_forwarding_lifetimes() {
21432143

21442144
fn foo<'a, P>(p: P)
21452145
where
2146-
for<'b> &'b P: Pattern<'a>,
2146+
for<'b> &'b P: Pattern<&'a str>,
21472147
{
21482148
for _ in 0..3 {
21492149
"asdf".find(&p);

0 commit comments

Comments
 (0)