-
Couldn't load subscription status.
- Fork 13.9k
constify from_fn, try_from_fn, try_map, map #147071
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
Open
bend-n
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
bend-n:const_array-ops
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−92
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,61 @@ | ||
| use crate::iter::{TrustedLen, UncheckedIterator}; | ||
| use crate::assert_unsafe_precondition; | ||
| use crate::marker::Destruct; | ||
| use crate::mem::ManuallyDrop; | ||
| use crate::ptr::drop_in_place; | ||
| use crate::slice; | ||
|
|
||
| /// A situationally-optimized version of `array.into_iter().for_each(func)`. | ||
| /// | ||
| /// [`crate::array::IntoIter`]s are great when you need an owned iterator, but | ||
| /// storing the entire array *inside* the iterator like that can sometimes | ||
| /// pessimize code. Notable, it can be more bytes than you really want to move | ||
| /// around, and because the array accesses index into it SRoA has a harder time | ||
| /// optimizing away the type than it does iterators that just hold a couple pointers. | ||
| /// | ||
| /// Thus this function exists, which gives a way to get *moved* access to the | ||
| /// elements of an array using a small iterator -- no bigger than a slice iterator. | ||
| /// | ||
| /// The function-taking-a-closure structure makes it safe, as it keeps callers | ||
| /// from looking at already-dropped elements. | ||
| pub(crate) fn drain_array_with<T, R, const N: usize>( | ||
| array: [T; N], | ||
| func: impl for<'a> FnOnce(Drain<'a, T>) -> R, | ||
| ) -> R { | ||
| let mut array = ManuallyDrop::new(array); | ||
| // SAFETY: Now that the local won't drop it, it's ok to construct the `Drain` which will. | ||
| let drain = Drain(array.iter_mut()); | ||
| func(drain) | ||
| #[rustc_const_unstable(feature = "array_try_map", issue = "79711")] | ||
| #[unstable(feature = "array_try_map", issue = "79711")] | ||
| pub(super) struct Drain<'a, T, U, const N: usize, F: FnMut(T) -> U> { | ||
| array: ManuallyDrop<[T; N]>, | ||
| moved: usize, | ||
| f: &'a mut F, | ||
| } | ||
|
|
||
| /// See [`drain_array_with`] -- this is `pub(crate)` only so it's allowed to be | ||
| /// mentioned in the signature of that method. (Otherwise it hits `E0446`.) | ||
| // INVARIANT: It's ok to drop the remainder of the inner iterator. | ||
| pub(crate) struct Drain<'a, T>(slice::IterMut<'a, T>); | ||
|
|
||
| impl<T> Drop for Drain<'_, T> { | ||
| fn drop(&mut self) { | ||
| // SAFETY: By the type invariant, we're allowed to drop all these. | ||
| unsafe { drop_in_place(self.0.as_mut_slice()) } | ||
| #[rustc_const_unstable(feature = "array_try_map", issue = "79711")] | ||
| #[unstable(feature = "array_try_map", issue = "79711")] | ||
| impl<T, U, const N: usize, F> const FnOnce<(usize,)> for &mut Drain<'_, T, U, N, F> | ||
| where | ||
| F: [const] FnMut(T) -> U, | ||
| { | ||
| type Output = U; | ||
|
|
||
| extern "rust-call" fn call_once(mut self, args: (usize,)) -> Self::Output { | ||
| self.call_mut(args) | ||
| } | ||
| } | ||
|
|
||
| impl<T> Iterator for Drain<'_, T> { | ||
| type Item = T; | ||
|
|
||
| #[inline] | ||
| fn next(&mut self) -> Option<T> { | ||
| let p: *const T = self.0.next()?; | ||
| // SAFETY: The iterator was already advanced, so we won't drop this later. | ||
| Some(unsafe { p.read() }) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let n = self.len(); | ||
| (n, Some(n)) | ||
| #[rustc_const_unstable(feature = "array_try_map", issue = "79711")] | ||
| #[unstable(feature = "array_try_map", issue = "79711")] | ||
| impl<T, U, const N: usize, F> const FnMut<(usize,)> for &mut Drain<'_, T, U, N, F> | ||
| where | ||
| F: [const] FnMut(T) -> U, | ||
| { | ||
| extern "rust-call" fn call_mut(&mut self, (i,): (usize,)) -> Self::Output { | ||
| // SAFETY: increment moved before moving. if `f` panics, we drop the rest. | ||
| self.moved += 1; | ||
| assert_unsafe_precondition!( | ||
| check_library_ub, | ||
| "musnt index array out of bounds", (i: usize = i, size: usize = N) => i < size | ||
| ); | ||
| // SAFETY: caller guarantees never called with number >= N (see `Drain::new`) | ||
| (self.f)(unsafe { self.array.as_ptr().add(i).read() }) | ||
| } | ||
| } | ||
|
|
||
| impl<T> ExactSizeIterator for Drain<'_, T> { | ||
| #[inline] | ||
| fn len(&self) -> usize { | ||
| self.0.len() | ||
| #[rustc_const_unstable(feature = "array_try_map", issue = "79711")] | ||
| #[unstable(feature = "array_try_map", issue = "79711")] | ||
| impl<T: [const] Destruct, U, const N: usize, F: FnMut(T) -> U> const Drop | ||
| for Drain<'_, T, U, N, F> | ||
| { | ||
| fn drop(&mut self) { | ||
| let mut n = self.moved; | ||
| while n != N { | ||
| // SAFETY: moved must always be < N | ||
| unsafe { self.array.as_mut_ptr().add(n).drop_in_place() }; | ||
| n += 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // SAFETY: This is a 1:1 wrapper for a slice iterator, which is also `TrustedLen`. | ||
| unsafe impl<T> TrustedLen for Drain<'_, T> {} | ||
|
|
||
| impl<T> UncheckedIterator for Drain<'_, T> { | ||
| unsafe fn next_unchecked(&mut self) -> T { | ||
| // SAFETY: `Drain` is 1:1 with the inner iterator, so if the caller promised | ||
| // that there's an element left, the inner iterator has one too. | ||
| let p: *const T = unsafe { self.0.next_unchecked() }; | ||
| // SAFETY: The iterator was already advanced, so we won't drop this later. | ||
| unsafe { p.read() } | ||
| impl<'a, T, U, const N: usize, F: FnMut(T) -> U> Drain<'a, T, U, N, F> { | ||
| /// SAFETY: must be called without indexing out of bounds. (see `Drain::call_mut`) | ||
| // FIXME(const-hack): this is a hack for `let guard = Guard(array); |i| f(guard[i])`. | ||
| pub(super) const unsafe fn new(array: [T; N], f: &'a mut F) -> Self { | ||
| Self { array: ManuallyDrop::new(array), moved: 0, f } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 would be possible to ignore this
iand just usemoved, but, uh, i think that would be somewhat confusing.