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

polyfill: Rewrite as_chunks/as_chunks_mut/flatten/flatten_mut. #2287

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

briansmith
Copy link
Owner

Define new types AsChunks<T,N> & AsChunksMut<T,N> that act like &[[T; N]] and &mut [[T; N]], respectively. Although there is a ton of boilerplate to do this, the advantage is we eliminate the unsafe in the implementations of as_chunks and as_chunks_mut, respectively.

It turns out we only have flatten/flatten_mut to undo the effects of as_chunks/as_chunks_mut. The new polyfills have a very natural implementation of flattening: just return the inner slice. Thus, we eliminate the prior use of unsafe in flattening easily.

core renamed flatten and flatten_mut to as_flattened and as_flattened_mut, respectively. Use the new naming.

Move as_chunks and related machinery into its own file, and do the same for as_chunks_mut. Then we can more easily compare the two implementations for (in)consistency.

diff src/polyfill/slice/as_chunks.rs \
     src/polyfill/slice/as_chunks_mut.rs

Copy link

codecov bot commented Jan 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.76%. Comparing base (0db51a8) to head (ae90b0a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2287      +/-   ##
==========================================
+ Coverage   96.72%   96.76%   +0.03%     
==========================================
  Files         171      173       +2     
  Lines       20843    20872      +29     
  Branches      489      487       -2     
==========================================
+ Hits        20161    20196      +35     
+ Misses        577      575       -2     
+ Partials      105      101       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Define new types `AsChunks<T,N>` & `AsChunksMut<T,N>` that act
like `&[[T; N]]` and `&mut [[T; N]]`, respectively. Although
there is a ton of boilerplate to do this, the advantage is we
eliminate the `unsafe` in the implementations of `as_chunks` and
`as_chunks_mut`, respectively.

It turns out we only have `flatten`/`flatten_mut` to undo the
effects of `as_chunks`/`as_chunks_mut`. The new polyfills have a
very natural implementation of flattening: just return the inner
slice. Thus, we eliminate the prior use of `unsafe` in
flattening easily.

`core` renamed `flatten` and `flatten_mut` to `as_flattened` and
`as_flattened_mut`, respectively. Use the new naming.

Move `as_chunks` and related machinery into its own file, and do
the same for `as_chunks_mut`. Then we can more easily compare
the two implementations for (in)consistency.

```
diff src/polyfill/slice/as_chunks.rs \
     src/polyfill/slice/as_chunks_mut.rs
```
@briansmith briansmith merged commit 63dc1e5 into main Jan 25, 2025
169 checks passed
@briansmith briansmith deleted the b/as_chunks branch January 25, 2025 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant