Skip to content

Generalize std::str::Pattern to be generic over the haystack and rehome it - #161606

Draft
pacak wants to merge 3 commits into
rust-lang:mainfrom
pacak:generalize-pattern
Draft

Generalize std::str::Pattern to be generic over the haystack and rehome it#161606
pacak wants to merge 3 commits into
rust-lang:mainfrom
pacak:generalize-pattern

Conversation

@pacak

@pacak pacak commented Aug 23, 2026

Copy link
Copy Markdown
Contributor
  1. Add a type parameter to Pattern so it can work with other haystack types (my goal is OsStr specifically)
  2. Move the whole thing from core::str::Pattern to core::pattern::Pattern to reflect that

Not very useful on its own, but required to extend pattern splitting API to OsStr.

Contains changes from #161604, I'll rebase one that test lands.

Part of the #160971 cinematic universe.

@rustbot

rustbot commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 23, 2026
@rustbot

rustbot commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, nia-e

@pacak

pacak commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

stdarch is developed in its own repository

I'm not sure what's the right approach here. Sync it after this lands? A separate pull request?

@folkertdev

Copy link
Copy Markdown
Contributor

This is fine, it's a tiny change, we'll handle the sync after it merges.

@pacak

pacak commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

r? @nia-e

@rustbot rustbot assigned nia-e and unassigned JohnTitor Aug 23, 2026
@pacak

pacak commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

#161608 (review) - I'm going to try this

pacak added 3 commits August 24, 2026 03:47
Right now things are undertested and underspecified.

Some of the library code would get in a loop if searcher starts
returning empty rejects.

And there's no tests for backwards multi byte char matchers. Pull
request I'm reviving had a problem implementing that, so making sure
it's tested before the actual code lands.

Right now it is possible to break both tests (and user code) without
breaking anything else in the test suite I think.
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.

@pacak: I moved some (or all new) of the `P: Pattern<&'a str>
constraints into where clause to keep things narrower:

```
pub fn foo<'a, P: Pattern<&'a str>>(&'a self, pat: P, ...) ...
```

to

```
pub fn replacen<'a, P>(&'a self, pat: P, ...) ...
     where
         P: Pattern<&'a str>,
```

Original code had indices in Haystack abstracted as an associated type
Cursor. Replaced with usize - Cursor adds noise with not much value.

Changed wording in 2-3 places - for example Searcher is generic over a
few types so it makes more sense to talk about split points in general
with utf8 split points as an example for `&str`.
Pattern is no longer str-specific, so move it from core::str::pattern
module to a new core::pattern module.  This introduces no changes in
behaviour or implementation.  Just moves stuff around and adjusts
documentation.
@pacak
pacak force-pushed the generalize-pattern branch from 9471adf to 0a4f4c4 Compare August 24, 2026 09:41
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@pacak
pacak marked this pull request as draft August 24, 2026 13:12
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants