Skip to content
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

Don't panic in ceil_char_boundary #112387

Merged
merged 3 commits into from Aug 15, 2023

Conversation

clarfonthey
Copy link
Contributor

Implementing the alternative mentioned in this comment: #93743 (comment)

Since floor_char_boundary will always work (rounding down to the length of the string is possible), it feels best for ceil_char_boundary to not panic either. However, the semantics of "rounding up" past the length of the string aren't very great, which is why the method originally panicked in these cases.

Taking into account how people are using this method, it feels best to simply return the end of the string in these cases, so that the result is still a valid char boundary.

@rustbot
Copy link
Collaborator

rustbot commented Jun 7, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 7, 2023
@rust-log-analyzer

This comment has been minimized.

@cuviper
Copy link
Member

cuviper commented Jun 16, 2023

I think this needs to be an API question, even though it's not stable yet. It seems surprising to me that it would return something less than index when it is stated up front, "not below index", so panicking is the only choice. But I also kind of think that floor should not paper over out-of-bounds index either.

@bors label -T-libs +T-libs-api
r? libs-api

@rustbot rustbot assigned Amanieu and unassigned cuviper Jun 16, 2023
@clarfonthey
Copy link
Contributor Author

For a more direct link to what I was searching: https://github.com/search?utf8=%E2%9C%93&q=ceil_char_boundary+language%3ARust&type=code&l=Rust

Essentially, I saw a few uses of ceil_char_boundary(index + 1) or something to that effect, and while I didn't scrutinise the exact usages to make sure they were using it properly, that alongside the combination of people asking for a non-panicking version made me feel that it would make the most sense for this to not panic.

My (flimsy) logic is to think of it like climbing a ladder, and if you climb past the last rung on the ladder you just stay at the last rung.

@Amanieu Amanieu added the I-libs-api-nominated The issue / PR has been nominated for discussion during a libs-api team meeting. label Aug 4, 2023
Co-authored-by: Josh Stone <cuviper@gmail.com>
@m-ou-se
Copy link
Member

m-ou-se commented Aug 15, 2023

We discussed this in the libs-api meeting last week. While we felt it was a bit odd for this method to return a value lower than its input, we agree that one of its main use cases is basically to sanitize input to e.g. s.split_at(x), s[..x], etc. The guarantee that any return value from floor_char_boundary and ceil_char_boundary is always a valid index to give to split_at is a very useful one.

Since this is still unstable, this PR doesn't require an FCP. (The behavior of this method will be part of the FCP that will happen when stabilizing it.)

@bors r+

@bors
Copy link
Contributor

bors commented Aug 15, 2023

📌 Commit 2f75dd4 has been approved by m-ou-se

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2023
@m-ou-se m-ou-se removed the I-libs-api-nominated The issue / PR has been nominated for discussion during a libs-api team meeting. label Aug 15, 2023
@bors
Copy link
Contributor

bors commented Aug 15, 2023

⌛ Testing commit 2f75dd4 with merge 4f4dae0...

@bors
Copy link
Contributor

bors commented Aug 15, 2023

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing 4f4dae0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 15, 2023
@bors bors merged commit 4f4dae0 into rust-lang:master Aug 15, 2023
12 checks passed
@rustbot rustbot added this to the 1.73.0 milestone Aug 15, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4f4dae0): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.5% [2.1%, 2.8%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.5% [2.1%, 2.8%] 3

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 633.856s -> 632.472s (-0.22%)
Artifact size: 346.73 MiB -> 346.76 MiB (0.01%)

@clarfonthey clarfonthey deleted the non-panicking-ceil-char-boundary branch September 16, 2023 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet

8 participants