-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StrExt::splitn should not require a DoubleEndedSearcher #23269
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
This has a couple of unanswered questions, so consider it a draft commit. I don't like having to add the duplication involved by creating |
@@ -1416,11 +1455,12 @@ impl StrExt for str { | |||
} | |||
|
|||
#[inline] | |||
fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> { | |||
fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> | |||
where P::Searcher: Searcher<'a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the associated type on the Pattern
trait already has this bound, so shouldn't this extra clause be unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly, it is unnecessary, but having it here provide much better error messages. If you have this here, the error message lets you know that this call needs to be changed. Otherwise, the return type simply won't implement Iterator
and you end up with "foo doesn't implement a method called {collect,map,etc}, try implementing Iterator".
cc @Kimundi, I vaguely remember there being a reason there may be a more strict requirement in some places? Not sure if this was intentional or accidental though. |
I'm sorry, but I'm unhappy with most of this PR. :(
So, in summary I'd be fine with a fix to make I'd much rather see the compiler solve the error message problem in a general way, eg by deeply searching for the cause of why a trait might not be implemented:
|
No need to be sorry - that's the point of reviews!
This was a question in my original comment. I actually agree with this decision, but didn't want to change the semantics until someone knowledgable agreed.
Easily done. I'll amend this commit; should I open a topic on internals for the rest of it? Maybe as proto RFC? Not so sure on the current best process. |
Hm, I think the motivation is applicable broadly enough that a internals discuss thread would be the best starting point. |
I doesn't sense to me to leave the correct bound off of the |
For pedantry's sake, after this PR that will be true. |
a7c7f11
to
da68868
Compare
@Kimundi I've got a new version up now, if you'd be so kind as to give it another look-see. The big thing that's new is that I've introduced I should add dedicated tests to both commits, however. |
@sfackler:
That said, while writing this I changed mind: I just realized that we can preserve consistency by treating DoubleEndedIterator impls and reverse iterators differently, but uniformly so.
... and this, apparently is also how @shepmaster interpreted the situation, so yeah, patch looks good! Tests should definitely be added, but I wouldn't fuss too much about code layout and the code duplication introduced here: I've started working on a patch that adds the missing methods from the string pattern RFC, which also includes the changes here, and in which I try to reduce the general complexity of forward/backward iterator wrappers with a more specialized macro that generates them. Not sure when that will be done though, so I'd definitely try to get this PR merged first. |
da68868
to
a39209e
Compare
@Kimundi Updated with tests and small doc changes. I think it's good-to-go now. Thanks! |
☔ The latest upstream changes (presumably #23104) made this pull request unmergeable. Please resolve the merge conflicts. |
a39209e
to
c6ca220
Compare
@Kimundi @alexcrichton updated to address merge conflicts. |
Looks good to me |
/// assert_eq!(v, ["ghi", "def", "abc"]); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I missed the context here, but the original motivation for this removal was that it duplicates the functionality of split(foo).rev()
(whereas rsplitn
has different semantics than splitn(foo).rev()
). Was this added back just to get nicer error messages on the .rev()
case? If so that may be covered by #23587 instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was that it duplicates the functionality of split(foo).rev()
@alexcrichton With the conceptual difference of Searcher
and ReverseSearcher
, this is no longer true. For example, these are not the same:
"baaab".split("aa").rev();
"baaab".rsplit("aa");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right! I always forget about that...
Closes #23262