-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Improve design of assert_len
#81154
Improve design of assert_len
#81154
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Name bikeshedding: another option might be |
This comment has been minimized.
This comment has been minimized.
This PR has been updated to fix #81157 by moving the method to |
Now that the method's defined on |
This adds [`Range::ensure_subset_of`]. | ||
|
||
[#76393]: https://github.com/rust-lang/rust/issues/76393 | ||
[`Range::ensure_subset_of`]: https://doc.rust-lang.org/std/ops/struct.Range.html#method.ensure_subset_of |
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 believe this should link to nightly since this is a nightly-only feature:
[`Range::ensure_subset_of`]: https://doc.rust-lang.org/std/ops/struct.Range.html#method.ensure_subset_of | |
[`Range::ensure_subset_of`]: https://doc.rust-lang.org/nightly/std/ops/struct.Range.html#method.ensure_subset_of |
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.
Could also just delete the file, since library features don't usually add files here -- they just use auto-generated ones.
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.
Can't this be a relative link?
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.
@scottmcm I only added it based on the link in tracking issue templates. However, that page is a little confusing, so I might be misunderstanding its recommendation.
Since this page is mostly a link to the tracking issue, removing it makes sense.
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.
@RalfJung Yes, that's a good idea. However, I removed the page instead since it's not very useful and seems to not be required.
I would move for |
Since this is a constructor on |
I hate to continue the bikeshedding even further, but I had to stare a long time at 6da4baeb094c to actually understand how it fixed the soundness. I would have expected the name to become something more like (though in that case the second |
Ah, sorry I missed that it was a constructor. I agree. Though I still don't like "subset". I like |
I think it'd be good for there to be the |
Can we get the best of both worlds with a custom Error? |
This is a good option, but it might distract from what this method's meant to do. It might be better to remove
I agree with @ExpHP about naming this method as a constructor. What does everyone think about That method can return |
Hmm, if it has to move off |
Has it been considered to simply return an empty range instead of panicking or None? I think this would make the method nice to use. You can check let vec = vec![1, 2, 3];
let range = (4..5).assert_len(3); // returns 3..3
for n in &vec[range] {
unreachable!("the slice is empty");
} A panicking version would still be useful for validating input. But I'm starting to think that an Option/Result version might be the least useful in practice. |
Note that an empty range is not a usable sentinel here, since |
I'm not sure what you mean. My suggestion should not impact |
Quite honestly this makes some pretty strong assumptions about how the user intends to use the range. In rust, the general expectation is that if a user supplies a range (I'm also just not generally sure what you're suggesting here. If somebody did |
Well maybe it was a bad idea 😬. But it would be |
That makes sense. Even though it's not a slice method anymore, you almost always want to use it for slices. Should we go back to
This method is designed to be very strict, so that wouldn't be compatible, but it might be useful as another method. One reason the method panics is to create helpful error messages with little effort. |
Moving to an inherent method in // panics if r isn't a subset of ..self.len()
let Range { start, end } = slice::range_from_subset(r, ..self.len()); I think there are really two things this method does which makes it tricky to name:
I think I'm more inclined towards a |
Yes, this is the problem. All the names given so far either show that the method panics or that it creates a range but not both. That's why I thought returning
If we add a |
… r=KodrAus Improve design of `assert_len` It was discussed in the [tracking issue](rust-lang#76393 (comment)) that `assert_len`'s name and usage are confusing. This PR improves them based on a suggestion by `@scottmcm` in that issue. I also improved the documentation to make it clearer when you might want to use this method. Old example: ```rust let range = range.assert_len(slice.len()); ``` New example: ```rust let range = range.ensure_subset_of(..slice.len()); ``` Fixes rust-lang#81157
⌛ Testing commit dd1ab4c with merge f4dd191f29b4ad50b4602b82d7b4a5ca450951ad... |
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
@bors retry |
@dylni: 🔑 Insufficient privileges: not in try users |
@KodrAus This PR should be ready to merge again. |
@bors r=KodrAus |
📌 Commit fe4fe19 has been approved by |
… r=KodrAus Improve design of `assert_len` It was discussed in the [tracking issue](rust-lang#76393 (comment)) that `assert_len`'s name and usage are confusing. This PR improves them based on a suggestion by `@scottmcm` in that issue. I also improved the documentation to make it clearer when you might want to use this method. Old example: ```rust let range = range.assert_len(slice.len()); ``` New example: ```rust let range = range.ensure_subset_of(..slice.len()); ``` Fixes rust-lang#81157
Rollup of 12 pull requests Successful merges: - rust-lang#79423 (Enable smart punctuation) - rust-lang#81154 (Improve design of `assert_len`) - rust-lang#81235 (Improve suggestion for tuple struct pattern matching errors.) - rust-lang#81769 (Suggest `return`ing tail expressions that match return type) - rust-lang#81837 (Slight perf improvement on char::to_ascii_lowercase) - rust-lang#81969 (Avoid `cfg_if` in `std::os`) - rust-lang#81984 (Make WASI's `hard_link` behavior match other platforms.) - rust-lang#82091 (use PlaceRef abstractions more consistently) - rust-lang#82128 (add diagnostic items for OsString/PathBuf/Owned as well as to_vec on slice) - rust-lang#82166 (add s390x-unknown-linux-musl target) - rust-lang#82234 (Remove query parameters when skipping search results) - rust-lang#82255 (Make `treat_err_as_bug` Option<NonZeroUsize>) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
It was discussed in the tracking issue that
assert_len
's name and usage are confusing. This PR improves them based on a suggestion by @scottmcm in that issue.I also improved the documentation to make it clearer when you might want to use this method.
Old example:
New example:
Fixes #81157