-
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
Add as_rchunks
(and friends) to slices
#78818
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
library/core/src/slice/mod.rs
Outdated
/// ``` | ||
#[unstable(feature = "slice_as_chunks", issue = "74985")] | ||
#[inline] | ||
pub unsafe fn as_chunks_mut_unchecked<const N: usize>(&mut self) -> &mut [[T; N]] { |
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.
It wasn't obvious to me whether this should be as_chunks_mut_unchecked
or as_chunks_unchecked_mut
. I could see precedent for both.
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.
It looks like unchecked_mut
is more frequently used but mut_unchecked
sounds better to me 😆
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.
It looks like unchecked_mut
has more precedent in slice and iterator APIs so maybe we should roll with that.
I'm very sad that we have a nice mix of both unchecked_mut
and mut_unchecked
🥲
pub fn chunks4_with_remainder(x: &[u8]) -> (&[[u8; 4]], &[u8]) { | ||
// CHECK: and i64 %x.1, -4 | ||
// CHECK: and i64 %x.1, 3 | ||
// CHECK: lshr exact |
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 wanted to ensure I didn't accidentally introduce extra instructions with the refactor, so codegen test. It did change, but only from lshr
/and
/and
to and
/and
/lshr exact
, which is a thoroughly uninteresting difference.
r? @KodrAus |
Thanks @scottmcm! r=me with |
@bors r=KodrAus |
📌 Commit 132307c has been approved by |
Add `as_rchunks` (and friends) to slices `@est31` mentioned (rust-lang#76354 (comment)) that, for completeness, there needed to be an `as_chunks`-like method that chunks from the end (with the remainder at the beginning) like `rchunks` does. So here's a PR for `as_rchunks: &[T] -> (&[T], &[[T; N]])` and `as_rchunks_mut: &mut [T] -> (&mut [T], &mut [[T; N]])`. But as I was doing this and copy-pasting `from_raw_parts` calls, I thought that I should extract that into an unsafe method. It started out a private helper, but it seemed like `as_chunks_unchecked` could be reasonable as a "real" method, so I added docs and made it public. Let me know if you think it doesn't pull its weight.
@bors r- This failed in #81085 (comment): The job Click to see the possible cause of the failure (guessed by this bot)
|
@bors rollup=never Sigh, codegen tests. |
This fixed things the last time I had a problem like this. And plausibly will here too -- the check it's failing on is for the high bit being set in the length of the slice, which is a check that's only in a debug_assert.
@bors r=KodrAus |
📌 Commit 6bcaba9 has been approved by |
☀️ Test successful - checks-actions |
@est31 mentioned (#76354 (comment)) that, for completeness, there needed to be an
as_chunks
-like method that chunks from the end (with the remainder at the beginning) likerchunks
does.So here's a PR for
as_rchunks: &[T] -> (&[T], &[[T; N]])
andas_rchunks_mut: &mut [T] -> (&mut [T], &mut [[T; N]])
.But as I was doing this and copy-pasting
from_raw_parts
calls, I thought that I should extract that into an unsafe method. It started out a private helper, but it seemed likeas_chunks_unchecked
could be reasonable as a "real" method, so I added docs and made it public. Let me know if you think it doesn't pull its weight.