Skip to content

Commit 33e0f9b

Browse files
committed
Auto merge of #156737 - asder8215:double_ended_iterator_next_chunk_back, r=aapoalas
Implement `DoubleEndedIterator::next_chunk_back` This PR builds off on @vinDelphini's [PR](#151668) on introducing `DoubleEndedIterator::next_chunk_back` (which will be used in a follow up PR to optimize `ArrayChunks::try_rfold` once merged). This was in the works since late Jan and I thought to pick it up and get this over the finish line as vinDelphini has been away from the PR for a couple of months. I've made sure to pull from vinDelphini's branch to keep him authored on the commits he made. Other things introduced from this PR is specialization like done for `Iterator::next_chunk`, constifying some stuff, making a custom `DoubleEndedIterator::next_chunk_back` for `IntoIter` (since it also has a custom impl of `Iterator::next_chunk`), and a bunch of tests mirroring that of `Iterator::next_chunk`. There's no tracking issue for this yet, but the ACP for this introduction is [here](rust-lang/libs-team#734). I'll create the tracking issue for this soon. ---- Actually, can we have `DoubleEndedIterator::next_chunk_back` underneath [`Iterator::next_chunk` tracking issue](#98326)?
2 parents 4c9d2bf + ae8f56d commit 33e0f9b

7 files changed

Lines changed: 307 additions & 0 deletions

File tree

β€Žlibrary/alloc/src/lib.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#![feature(legacy_receiver_trait)]
145145
#![feature(likely_unlikely)]
146146
#![feature(local_waker)]
147+
#![feature(maybe_uninit_array_assume_init)]
147148
#![feature(maybe_uninit_uninit_array_transpose)]
148149
#![feature(panic_internals)]
149150
#![feature(pattern)]

β€Žlibrary/alloc/src/vec/into_iter.rsβ€Ž

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,47 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
445445
}
446446
}
447447

448+
#[inline]
449+
fn next_chunk_back<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
450+
let mut raw_ary = [const { MaybeUninit::uninit() }; N];
451+
452+
let len = self.len();
453+
454+
if T::IS_ZST {
455+
if len < N {
456+
self.forget_remaining_elements();
457+
// Safety: ZSTs can be conjured ex nihilo, only the amount has to be correct
458+
return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, N - len..N) });
459+
}
460+
461+
self.end = self.end.wrapping_byte_sub(N);
462+
// Safety: ditto
463+
return Ok(unsafe { MaybeUninit::array_assume_init(raw_ary) });
464+
}
465+
466+
if len < N {
467+
// Safety: `len` indicates that this many elements are available and we just checked that
468+
// it fits into the array.
469+
unsafe {
470+
ptr::copy_nonoverlapping(self.ptr.as_ptr(), raw_ary.as_mut_ptr() as *mut T, len);
471+
self.forget_remaining_elements();
472+
return Err(array::IntoIter::new_unchecked(raw_ary, 0..len));
473+
}
474+
}
475+
476+
// Safety: `len` is larger than the array size. Copy a fixed amount here to fully initialize
477+
// the array.
478+
unsafe {
479+
ptr::copy_nonoverlapping(
480+
self.ptr.add(len - N).as_ptr(),
481+
raw_ary.as_mut_ptr() as *mut T,
482+
N,
483+
);
484+
self.end = self.end.sub(N);
485+
Ok(MaybeUninit::array_assume_init(raw_ary))
486+
}
487+
}
488+
448489
#[inline]
449490
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
450491
let step_size = self.len().min(n);

β€Žlibrary/alloctests/tests/vec.rsβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,15 @@ fn test_into_iter_next_chunk() {
10271027
assert_eq!(iter.next_chunk::<4>().unwrap_err().as_slice(), &[]); // N is explicitly 4
10281028
}
10291029

1030+
#[test]
1031+
fn test_into_iter_next_chunk_back() {
1032+
let mut iter = b"lorem".to_vec().into_iter();
1033+
1034+
assert_eq!(iter.next_chunk_back().unwrap(), [b'e', b'm']); // N is inferred as 2
1035+
assert_eq!(iter.next_chunk_back().unwrap(), [b'l', b'o', b'r']); // N is inferred as 3
1036+
assert_eq!(iter.next_chunk_back::<4>().unwrap_err().as_slice(), &[]); // N is explicitly 4
1037+
}
1038+
10301039
#[test]
10311040
fn test_into_iter_clone() {
10321041
fn iter_equal<I: Iterator<Item = i32>>(it: I, slice: &[i32]) {
@@ -1131,6 +1140,14 @@ fn test_into_iter_zst() {
11311140
let mut it = vec![C, C].into_iter();
11321141
it.next_chunk::<4>().unwrap_err();
11331142
drop(it);
1143+
1144+
let mut it = vec![C, C].into_iter();
1145+
it.next_chunk_back::<1>().unwrap();
1146+
drop(it);
1147+
1148+
let mut it = vec![C, C].into_iter();
1149+
it.next_chunk_back::<4>().unwrap_err();
1150+
drop(it);
11341151
}
11351152

11361153
#[test]

β€Žlibrary/core/src/array/mod.rsβ€Ž

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,54 @@ const impl<T: [const] Destruct> Drop for Guard<'_, T> {
969969
}
970970
}
971971

972+
/// Panic guard for incremental initialization of arrays from the back.
973+
///
974+
/// Elements of the array are populated starting from the end towards the beginning.
975+
/// Disarm the guard with `mem::forget` once the array has been fully initialized.
976+
///
977+
/// # Safety
978+
///
979+
/// All write accesses to this structure are unsafe and must maintain a correct
980+
/// count of `initialized` elements.
981+
struct GuardBack<'a, T> {
982+
/// The array to be initialized (will be filled from the end).
983+
pub array_mut: &'a mut [MaybeUninit<T>],
984+
/// The number of items that have been initialized so far.
985+
pub initialized: usize,
986+
}
987+
988+
impl<T> GuardBack<'_, T> {
989+
/// Adds an item to the array and updates the initialized item counter.
990+
///
991+
/// # Safety
992+
///
993+
/// No more than N elements must be initialized.
994+
#[inline]
995+
pub(crate) unsafe fn push_unchecked(&mut self, item: T) {
996+
// SAFETY: If `initialized` was correct before and the caller does not
997+
// invoke this method more than N times, then writes will be in-bounds
998+
// and slots will not be initialized more than once.
999+
unsafe {
1000+
let offset = self.initialized.unchecked_add(1);
1001+
let index = self.array_mut.len().unchecked_sub(offset);
1002+
self.array_mut.get_unchecked_mut(index).write(item);
1003+
self.initialized = offset;
1004+
}
1005+
}
1006+
}
1007+
1008+
impl<T: Destruct> Drop for GuardBack<'_, T> {
1009+
#[inline]
1010+
fn drop(&mut self) {
1011+
debug_assert!(self.initialized <= self.array_mut.len());
1012+
let len = self.array_mut.len();
1013+
// SAFETY: this slice will contain only initialized objects.
1014+
unsafe {
1015+
self.array_mut.get_unchecked_mut(len - self.initialized..len).assume_init_drop();
1016+
}
1017+
}
1018+
}
1019+
9721020
/// Pulls `N` items from `iter` and returns them as an array. If the iterator
9731021
/// yields fewer than `N` items, `Err` is returned containing an iterator over
9741022
/// the already yielded items.
@@ -1076,3 +1124,109 @@ const fn iter_next_chunk_erased<T>(
10761124
mem::forget(guard);
10771125
Ok(())
10781126
}
1127+
1128+
/// Pulls `N` items from the back of `iter` and returns them as an array.
1129+
/// If the iterator yields fewer than `N` items, `Err` is returned containing
1130+
/// an iterator over the already yielded items.
1131+
///
1132+
/// Since the iterator is passed as a mutable reference and this function calls
1133+
/// `next_back` at most `N` times, the iterator can still be used afterwards to
1134+
/// retrieve the remaining items.
1135+
///
1136+
/// If `iter.next_back()` panics, all items already yielded by the iterator are
1137+
/// dropped.
1138+
///
1139+
/// Used for [`DoubleEndedIterator::next_chunk_back`].
1140+
#[inline]
1141+
pub(crate) fn iter_next_chunk_back<T, const N: usize>(
1142+
iter: &mut impl DoubleEndedIterator<Item = T>,
1143+
) -> Result<[T; N], IntoIter<T, N>> {
1144+
iter.spec_next_chunk_back()
1145+
}
1146+
1147+
pub(crate) trait SpecNextChunkBack<T, const N: usize>:
1148+
DoubleEndedIterator<Item = T>
1149+
{
1150+
fn spec_next_chunk_back(&mut self) -> Result<[T; N], IntoIter<T, N>>;
1151+
}
1152+
1153+
impl<I: DoubleEndedIterator<Item = T>, T, const N: usize> SpecNextChunkBack<T, N> for I {
1154+
#[inline]
1155+
default fn spec_next_chunk_back(&mut self) -> Result<[T; N], IntoIter<T, N>> {
1156+
let mut array = [const { MaybeUninit::uninit() }; N];
1157+
let r = iter_next_chunk_back_erased(&mut array, self);
1158+
match r {
1159+
Ok(()) => {
1160+
// SAFETY: All elements of `array` were populated.
1161+
Ok(unsafe { MaybeUninit::array_assume_init(array) })
1162+
}
1163+
Err(initialized) => {
1164+
// SAFETY: Only the last `initialized` elements were populated
1165+
Err(unsafe { IntoIter::new_unchecked(array, N - initialized..N) })
1166+
}
1167+
}
1168+
}
1169+
}
1170+
1171+
impl<I: DoubleEndedIterator<Item = T> + TrustedLen, T, const N: usize> SpecNextChunkBack<T, N>
1172+
for I
1173+
{
1174+
fn spec_next_chunk_back(&mut self) -> Result<[T; N], IntoIter<T, N>> {
1175+
let len = (*self).size_hint().0;
1176+
let mut array = [const { MaybeUninit::uninit() }; N];
1177+
if len < N {
1178+
// SAFETY: `TrustedLen`, an unsafe trait, requires that i can get len items out of it.
1179+
unsafe { write_back(&mut array, self, len) };
1180+
// SAFETY: Only the last `len` elements were populated
1181+
Err(unsafe { IntoIter::new_unchecked(array, N - len..N) })
1182+
} else {
1183+
// SAFETY: `TrustedLen`, an unsafe trait, requires that i can get N items out of it.
1184+
unsafe { write_back(&mut array, self, N) };
1185+
// SAFETY: All N items were populated
1186+
Ok(unsafe { MaybeUninit::array_assume_init(array) })
1187+
}
1188+
}
1189+
}
1190+
1191+
// SAFETY: `from` must have len items, and len items must be < N.
1192+
unsafe fn write_back<T, const N: usize>(
1193+
to: &mut [MaybeUninit<T>; N],
1194+
from: &mut impl DoubleEndedIterator<Item = T>,
1195+
len: usize,
1196+
) {
1197+
let mut guard = GuardBack { array_mut: to, initialized: 0 };
1198+
while guard.initialized < len {
1199+
// SAFETY: caller has guaranteed, from has len items.
1200+
let item = unsafe { from.next_back().unwrap_unchecked() };
1201+
// SAFETY: guard.initialized < len < N
1202+
unsafe { guard.push_unchecked(item) };
1203+
}
1204+
crate::mem::forget(guard);
1205+
}
1206+
1207+
/// Version of [`iter_next_chunk_back`] using a passed-in slice
1208+
/// in order to avoid needing to monomorphize for every array length.
1209+
///
1210+
/// Unfortunately this loop has two exit conditions, the buffer filling up
1211+
/// or the iterator running out of items, making it tend to optimize poorly.
1212+
#[inline]
1213+
fn iter_next_chunk_back_erased<T>(
1214+
buffer: &mut [MaybeUninit<T>],
1215+
iter: &mut impl DoubleEndedIterator<Item = T>,
1216+
) -> Result<(), usize> {
1217+
// if `Iterator::next_back` panics, this guard will drop already initialized items
1218+
let mut guard = GuardBack { array_mut: buffer, initialized: 0 };
1219+
while guard.initialized < guard.array_mut.len() {
1220+
let Some(item) = iter.next_back() else {
1221+
let initialized = guard.initialized;
1222+
mem::forget(guard);
1223+
return Err(initialized);
1224+
};
1225+
1226+
// SAFETY: The loop condition ensures we have space to push the item
1227+
unsafe { guard.push_unchecked(item) };
1228+
}
1229+
1230+
mem::forget(guard);
1231+
Ok(())
1232+
}

β€Žlibrary/core/src/iter/traits/double_ended.rsβ€Ž

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::array;
12
use crate::marker::Destruct;
23
use crate::num::NonZero;
34
use crate::ops::{ControlFlow, Try};
@@ -95,6 +96,53 @@ pub const trait DoubleEndedIterator: [const] Iterator {
9596
#[stable(feature = "rust1", since = "1.0.0")]
9697
fn next_back(&mut self) -> Option<Self::Item>;
9798

99+
/// Advances from the back of the iterator and returns an array containing the next
100+
/// `N` values in sequence.
101+
///
102+
/// If there are not enough elements to fill the array then `Err` is returned
103+
/// containing an iterator over the remaining elements.
104+
///
105+
/// Note: This is not equivalent to doing `iter.rev().next_chunk()` as this method
106+
/// takes elements from the back of the iterator and preserves the order that the
107+
/// elements were seen in the original iterator.
108+
///
109+
/// # Examples
110+
///
111+
/// Basic usage:
112+
///
113+
/// ```
114+
/// #![feature(iter_next_chunk)]
115+
///
116+
/// let mut iter = "lorem".chars();
117+
///
118+
/// assert_eq!(iter.next_chunk_back().unwrap(), ['e', 'm']); // N is inferred as 2
119+
/// assert_eq!(iter.next_chunk_back().unwrap(), ['l', 'o', 'r']); // N is inferred as 3
120+
/// assert_eq!(iter.next_chunk_back::<4>().unwrap_err().as_slice(), &[]); // N is explicitly 4
121+
/// ```
122+
///
123+
/// Split a string and get the last three items in sequence.
124+
///
125+
/// ```
126+
/// #![feature(iter_next_chunk)]
127+
///
128+
/// let quote = "not all those who wander are lost";
129+
/// let [first, second, third] = quote.split_whitespace().next_chunk_back().unwrap();
130+
/// assert_eq!(first, "wander");
131+
/// assert_eq!(second, "are");
132+
/// assert_eq!(third, "lost");
133+
/// ```
134+
#[inline]
135+
#[unstable(feature = "iter_next_chunk", issue = "98326")]
136+
#[rustc_non_const_trait_method]
137+
fn next_chunk_back<const N: usize>(
138+
&mut self,
139+
) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>>
140+
where
141+
Self: Sized,
142+
{
143+
crate::array::iter_next_chunk_back(self)
144+
}
145+
98146
/// Advances the iterator from the back by `n` elements.
99147
///
100148
/// `advance_back_by` is the reverse version of [`advance_by`]. This method will

β€Žlibrary/coretests/tests/iter/adapters/filter.rsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ fn test_next_chunk_does_not_leak() {
6363
assert_eq!(Rc::strong_count(w), 1);
6464
}
6565
}
66+
67+
#[test]
68+
fn test_next_chunk_back_does_not_leak() {
69+
let drop_witness: [_; 5] = std::array::from_fn(|_| Rc::new(()));
70+
71+
let v = (0..5).map(|i| drop_witness[i].clone()).collect::<Vec<_>>();
72+
let _ = v.into_iter().filter(|_| false).next_chunk_back::<1>();
73+
74+
for ref w in drop_witness {
75+
assert_eq!(Rc::strong_count(w), 1);
76+
}
77+
}

β€Žlibrary/coretests/tests/iter/traits/iterator.rsβ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,18 @@ fn test_next_chunk() {
619619
assert_eq!(it.next_chunk::<0>().unwrap(), []);
620620
}
621621

622+
#[test]
623+
fn test_next_chunk_back() {
624+
let mut it = 0..12;
625+
assert_eq!(it.next_chunk_back().unwrap(), [8, 9, 10, 11]);
626+
assert_eq!(it.next_chunk_back().unwrap(), []);
627+
assert_eq!(it.next_chunk_back().unwrap(), [2, 3, 4, 5, 6, 7]);
628+
assert_eq!(it.next_chunk_back::<4>().unwrap_err().as_slice(), &[0, 1]);
629+
630+
let mut it = std::iter::once_with(|| panic!());
631+
assert_eq!(it.next_chunk_back::<0>().unwrap(), []);
632+
}
633+
622634
#[test]
623635
fn test_next_chunk_untrusted() {
624636
struct Untrusted<I: Iterator>(I);
@@ -636,6 +648,28 @@ fn test_next_chunk_untrusted() {
636648
assert_eq!(it.next_chunk::<4>().unwrap_err().as_slice(), &[10, 11]);
637649
}
638650

651+
#[test]
652+
fn test_next_chunk_back_untrusted() {
653+
struct Untrusted<I: Iterator>(I);
654+
impl<I: Iterator> Iterator for Untrusted<I> {
655+
type Item = I::Item;
656+
657+
fn next(&mut self) -> Option<Self::Item> {
658+
self.0.next()
659+
}
660+
}
661+
impl<I: DoubleEndedIterator> DoubleEndedIterator for Untrusted<I> {
662+
fn next_back(&mut self) -> Option<Self::Item> {
663+
self.0.next_back()
664+
}
665+
}
666+
let mut it = Untrusted(0..12);
667+
assert_eq!(it.next_chunk_back().unwrap(), [8, 9, 10, 11]);
668+
assert_eq!(it.next_chunk_back().unwrap(), []);
669+
assert_eq!(it.next_chunk_back().unwrap(), [2, 3, 4, 5, 6, 7]);
670+
assert_eq!(it.next_chunk::<4>().unwrap_err().as_slice(), &[0, 1]);
671+
}
672+
639673
#[test]
640674
fn test_collect_into_tuples() {
641675
let a = vec![(1, 2, 3), (4, 5, 6), (7, 8, 9)];

0 commit comments

Comments
Β (0)