|
1 | | -use crate::iter::{TrustedLen, UncheckedIterator}; |
| 1 | +use crate::assert_unsafe_precondition; |
| 2 | +use crate::marker::Destruct; |
2 | 3 | use crate::mem::ManuallyDrop; |
3 | | -use crate::ptr::drop_in_place; |
4 | | -use crate::slice; |
5 | 4 |
|
6 | | -/// A situationally-optimized version of `array.into_iter().for_each(func)`. |
7 | | -/// |
8 | | -/// [`crate::array::IntoIter`]s are great when you need an owned iterator, but |
9 | | -/// storing the entire array *inside* the iterator like that can sometimes |
10 | | -/// pessimize code. Notable, it can be more bytes than you really want to move |
11 | | -/// around, and because the array accesses index into it SRoA has a harder time |
12 | | -/// optimizing away the type than it does iterators that just hold a couple pointers. |
13 | | -/// |
14 | | -/// Thus this function exists, which gives a way to get *moved* access to the |
15 | | -/// elements of an array using a small iterator -- no bigger than a slice iterator. |
16 | | -/// |
17 | | -/// The function-taking-a-closure structure makes it safe, as it keeps callers |
18 | | -/// from looking at already-dropped elements. |
19 | | -pub(crate) fn drain_array_with<T, R, const N: usize>( |
20 | | - array: [T; N], |
21 | | - func: impl for<'a> FnOnce(Drain<'a, T>) -> R, |
22 | | -) -> R { |
23 | | - let mut array = ManuallyDrop::new(array); |
24 | | - // SAFETY: Now that the local won't drop it, it's ok to construct the `Drain` which will. |
25 | | - let drain = Drain(array.iter_mut()); |
26 | | - func(drain) |
| 5 | +#[rustc_const_unstable(feature = "array_try_map", issue = "79711")] |
| 6 | +#[unstable(feature = "array_try_map", issue = "79711")] |
| 7 | +pub(super) struct Drain<'a, T, U, const N: usize, F: FnMut(T) -> U> { |
| 8 | + array: ManuallyDrop<[T; N]>, |
| 9 | + moved: usize, |
| 10 | + f: &'a mut F, |
27 | 11 | } |
| 12 | +#[rustc_const_unstable(feature = "array_try_map", issue = "79711")] |
| 13 | +#[unstable(feature = "array_try_map", issue = "79711")] |
| 14 | +impl<T, U, const N: usize, F> const FnOnce<(usize,)> for &mut Drain<'_, T, U, N, F> |
| 15 | +where |
| 16 | + F: [const] FnMut(T) -> U, |
| 17 | +{ |
| 18 | + type Output = U; |
28 | 19 |
|
29 | | -/// See [`drain_array_with`] -- this is `pub(crate)` only so it's allowed to be |
30 | | -/// mentioned in the signature of that method. (Otherwise it hits `E0446`.) |
31 | | -// INVARIANT: It's ok to drop the remainder of the inner iterator. |
32 | | -pub(crate) struct Drain<'a, T>(slice::IterMut<'a, T>); |
33 | | - |
34 | | -impl<T> Drop for Drain<'_, T> { |
35 | | - fn drop(&mut self) { |
36 | | - // SAFETY: By the type invariant, we're allowed to drop all these. |
37 | | - unsafe { drop_in_place(self.0.as_mut_slice()) } |
| 20 | + extern "rust-call" fn call_once(mut self, args: (usize,)) -> Self::Output { |
| 21 | + self.call_mut(args) |
38 | 22 | } |
39 | 23 | } |
40 | | - |
41 | | -impl<T> Iterator for Drain<'_, T> { |
42 | | - type Item = T; |
43 | | - |
44 | | - #[inline] |
45 | | - fn next(&mut self) -> Option<T> { |
46 | | - let p: *const T = self.0.next()?; |
47 | | - // SAFETY: The iterator was already advanced, so we won't drop this later. |
48 | | - Some(unsafe { p.read() }) |
49 | | - } |
50 | | - |
51 | | - #[inline] |
52 | | - fn size_hint(&self) -> (usize, Option<usize>) { |
53 | | - let n = self.len(); |
54 | | - (n, Some(n)) |
| 24 | +#[rustc_const_unstable(feature = "array_try_map", issue = "79711")] |
| 25 | +#[unstable(feature = "array_try_map", issue = "79711")] |
| 26 | +impl<T, U, const N: usize, F> const FnMut<(usize,)> for &mut Drain<'_, T, U, N, F> |
| 27 | +where |
| 28 | + F: [const] FnMut(T) -> U, |
| 29 | +{ |
| 30 | + extern "rust-call" fn call_mut(&mut self, (i,): (usize,)) -> Self::Output { |
| 31 | + // SAFETY: increment moved before moving. if `f` panics, we drop the rest. |
| 32 | + self.moved += 1; |
| 33 | + assert_unsafe_precondition!( |
| 34 | + check_library_ub, |
| 35 | + "musnt index array out of bounds", (i: usize = i, size: usize = N) => i < size |
| 36 | + ); |
| 37 | + // SAFETY: the `i` should also always go up, and musnt skip any, else some things will be leaked. |
| 38 | + // SAFETY: if it goes down, we will drop freed elements. not good. |
| 39 | + // SAFETY: caller guarantees never called with number >= N (see `Drain::new`) |
| 40 | + (self.f)(unsafe { self.array.as_ptr().add(i).read() }) |
55 | 41 | } |
56 | 42 | } |
57 | | - |
58 | | -impl<T> ExactSizeIterator for Drain<'_, T> { |
59 | | - #[inline] |
60 | | - fn len(&self) -> usize { |
61 | | - self.0.len() |
| 43 | +#[rustc_const_unstable(feature = "array_try_map", issue = "79711")] |
| 44 | +#[unstable(feature = "array_try_map", issue = "79711")] |
| 45 | +impl<T: [const] Destruct, U, const N: usize, F: FnMut(T) -> U> const Drop |
| 46 | + for Drain<'_, T, U, N, F> |
| 47 | +{ |
| 48 | + fn drop(&mut self) { |
| 49 | + let mut n = self.moved; |
| 50 | + while n != N { |
| 51 | + // SAFETY: moved must always be < N |
| 52 | + unsafe { self.array.as_mut_ptr().add(n).drop_in_place() }; |
| 53 | + n += 1; |
| 54 | + } |
62 | 55 | } |
63 | 56 | } |
64 | | - |
65 | | -// SAFETY: This is a 1:1 wrapper for a slice iterator, which is also `TrustedLen`. |
66 | | -unsafe impl<T> TrustedLen for Drain<'_, T> {} |
67 | | - |
68 | | -impl<T> UncheckedIterator for Drain<'_, T> { |
69 | | - unsafe fn next_unchecked(&mut self) -> T { |
70 | | - // SAFETY: `Drain` is 1:1 with the inner iterator, so if the caller promised |
71 | | - // that there's an element left, the inner iterator has one too. |
72 | | - let p: *const T = unsafe { self.0.next_unchecked() }; |
73 | | - // SAFETY: The iterator was already advanced, so we won't drop this later. |
74 | | - unsafe { p.read() } |
| 57 | +impl<'a, T, U, const N: usize, F: FnMut(T) -> U> Drain<'a, T, U, N, F> { |
| 58 | + /// This function returns a function that lets you index the given array in const. |
| 59 | + /// As implemented it can optimize better than iterators, and can be constified. |
| 60 | + /// It acts like a sort of guard and iterator combined, which can be implemented |
| 61 | + /// as it is a struct that implements const fn; |
| 62 | + /// in that regard it is somewhat similar to an array::Iter implementing `UncheckedIterator`. |
| 63 | + /// The only method you're really allowed to call is `next()`, |
| 64 | + /// anything else is more or less UB, hence this function being unsafe. |
| 65 | + /// Moved elements will not be dropped. |
| 66 | + /// |
| 67 | + /// Previously this was implemented as a wrapper around a `slice::Iter`, which |
| 68 | + /// called `read()` on the returned `&T`; gnarly stuff. |
| 69 | + /// |
| 70 | + /// SAFETY: must be called in order of 0..N, without indexing out of bounds. (see `Drain::call_mut`) |
| 71 | + /// Potentially the function could completely disregard the supplied argument, however i think that behaviour would be unintuitive. |
| 72 | + // FIXME(const-hack): this is a hack for `let guard = Guard(array); |i| f(guard[i])`. |
| 73 | + pub(super) const unsafe fn new(array: [T; N], f: &'a mut F) -> Self { |
| 74 | + Self { array: ManuallyDrop::new(array), moved: 0, f } |
75 | 75 | } |
76 | 76 | } |
0 commit comments