Skip to content

Commit 80282ba

Browse files
committed
Rename Iterator::array_chunks to Iterator::chunks
The most logical way to yield chunks from an iterator is with arrays. Originally I named this to align with `slice::array_chunks` but I think `chunks` makes more sense for iterators and it aligns with the `next_chunk` method.
1 parent 0e9eee6 commit 80282ba

File tree

8 files changed

+66
-66
lines changed

8 files changed

+66
-66
lines changed

library/core/benches/iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ fn bench_copied_chunks(b: &mut Bencher) {
419419
})
420420
}
421421

422-
/// Exercises the TrustedRandomAccess specialization in ArrayChunks
422+
/// Exercises the TrustedRandomAccess specialization in Chunks
423423
#[bench]
424424
fn bench_trusted_random_access_chunks(b: &mut Bencher) {
425425
let v = vec![1u8; 1024];
@@ -429,7 +429,7 @@ fn bench_trusted_random_access_chunks(b: &mut Bencher) {
429429
.iter()
430430
// this shows that we're not relying on the slice::Iter specialization in Copied
431431
.map(|b| *b.borrow())
432-
.array_chunks::<{ mem::size_of::<u64>() }>()
432+
.chunks::<{ mem::size_of::<u64>() }>()
433433
.map(|ary| {
434434
let d = u64::from_ne_bytes(ary);
435435
Wrapping(d.rotate_left(7).wrapping_add(1))

library/core/src/iter/adapters/array_chunks.rs renamed to library/core/src/iter/adapters/chunks.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ use crate::ops::{ControlFlow, NeverShortCircuit, Try};
99
/// The chunks do not overlap. If `N` does not divide the length of the
1010
/// iterator, then the last up to `N-1` elements will be omitted.
1111
///
12-
/// This `struct` is created by the [`array_chunks`][Iterator::array_chunks]
13-
/// method on [`Iterator`]. See its documentation for more.
12+
/// This `struct` is created by the [`chunks`][Iterator::chunks] method on
13+
/// [`Iterator`]. See its documentation for more.
1414
#[derive(Debug, Clone)]
1515
#[must_use = "iterators are lazy and do nothing unless consumed"]
1616
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
17-
pub struct ArrayChunks<I: Iterator, const N: usize> {
17+
pub struct Chunks<I: Iterator, const N: usize> {
1818
iter: I,
1919
remainder: Option<array::IntoIter<I::Item, N>>,
2020
}
2121

22-
impl<I, const N: usize> ArrayChunks<I, N>
22+
impl<I, const N: usize> Chunks<I, N>
2323
where
2424
I: Iterator,
2525
{
@@ -40,7 +40,7 @@ where
4040
}
4141

4242
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
43-
impl<I, const N: usize> Iterator for ArrayChunks<I, N>
43+
impl<I, const N: usize> Iterator for Chunks<I, N>
4444
where
4545
I: Iterator,
4646
{
@@ -75,7 +75,7 @@ where
7575
Ok(chunk) => acc = f(acc, chunk)?,
7676
Err(remainder) => {
7777
// Make sure to not override `self.remainder` with an empty array
78-
// when `next` is called after `ArrayChunks` exhaustion.
78+
// when `next` is called after `Chunks` exhaustion.
7979
self.remainder.get_or_insert(remainder);
8080

8181
break try { acc };
@@ -94,7 +94,7 @@ where
9494
}
9595

9696
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
97-
impl<I, const N: usize> DoubleEndedIterator for ArrayChunks<I, N>
97+
impl<I, const N: usize> DoubleEndedIterator for Chunks<I, N>
9898
where
9999
I: DoubleEndedIterator + ExactSizeIterator,
100100
{
@@ -131,14 +131,14 @@ where
131131
impl_fold_via_try_fold! { rfold -> try_rfold }
132132
}
133133

134-
impl<I, const N: usize> ArrayChunks<I, N>
134+
impl<I, const N: usize> Chunks<I, N>
135135
where
136136
I: DoubleEndedIterator + ExactSizeIterator,
137137
{
138138
/// Updates `self.remainder` such that `self.iter.len` is divisible by `N`.
139139
fn next_back_remainder(&mut self) {
140140
// Make sure to not override `self.remainder` with an empty array
141-
// when `next_back` is called after `ArrayChunks` exhaustion.
141+
// when `next_back` is called after `Chunks` exhaustion.
142142
if self.remainder.is_some() {
143143
return;
144144
}
@@ -159,10 +159,10 @@ where
159159
}
160160

161161
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
162-
impl<I, const N: usize> FusedIterator for ArrayChunks<I, N> where I: FusedIterator {}
162+
impl<I, const N: usize> FusedIterator for Chunks<I, N> where I: FusedIterator {}
163163

164164
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
165-
impl<I, const N: usize> ExactSizeIterator for ArrayChunks<I, N>
165+
impl<I, const N: usize> ExactSizeIterator for Chunks<I, N>
166166
where
167167
I: ExactSizeIterator,
168168
{
@@ -184,7 +184,7 @@ trait SpecFold: Iterator {
184184
F: FnMut(B, Self::Item) -> B;
185185
}
186186

187-
impl<I, const N: usize> SpecFold for ArrayChunks<I, N>
187+
impl<I, const N: usize> SpecFold for Chunks<I, N>
188188
where
189189
I: Iterator,
190190
{
@@ -199,7 +199,7 @@ where
199199
}
200200
}
201201

202-
impl<I, const N: usize> SpecFold for ArrayChunks<I, N>
202+
impl<I, const N: usize> SpecFold for Chunks<I, N>
203203
where
204204
I: Iterator + TrustedRandomAccessNoCoerce,
205205
{

library/core/src/iter/adapters/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::iter::{InPlaceIterable, Iterator};
22
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
33

4-
mod array_chunks;
54
mod by_ref_sized;
65
mod chain;
6+
mod chunks;
77
mod cloned;
88
mod copied;
99
mod cycle;
@@ -34,7 +34,7 @@ pub use self::{
3434
};
3535

3636
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
37-
pub use self::array_chunks::ArrayChunks;
37+
pub use self::chunks::Chunks;
3838

3939
#[unstable(feature = "std_internals", issue = "none")]
4040
pub use self::by_ref_sized::ByRefSized;

library/core/src/iter/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,10 @@ pub use self::traits::{
423423

424424
#[stable(feature = "iter_zip", since = "1.59.0")]
425425
pub use self::adapters::zip;
426-
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
427-
pub use self::adapters::ArrayChunks;
428426
#[unstable(feature = "std_internals", issue = "none")]
429427
pub use self::adapters::ByRefSized;
428+
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
429+
pub use self::adapters::Chunks;
430430
#[stable(feature = "iter_cloned", since = "1.1.0")]
431431
pub use self::adapters::Cloned;
432432
#[stable(feature = "iter_copied", since = "1.36.0")]

library/core/src/iter/traits/iterator.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
55
use super::super::try_process;
66
use super::super::ByRefSized;
77
use super::super::TrustedRandomAccessNoCoerce;
8-
use super::super::{ArrayChunks, Chain, Cloned, Copied, Cycle, Enumerate, Filter, FilterMap, Fuse};
8+
use super::super::{Chain, Chunks, Cloned, Copied, Cycle, Enumerate, Filter, FilterMap, Fuse};
99
use super::super::{FlatMap, Flatten};
1010
use super::super::{FromIterator, Intersperse, IntersperseWith, Product, Sum, Zip};
1111
use super::super::{
@@ -3311,7 +3311,7 @@ pub trait Iterator {
33113311
///
33123312
/// The chunks do not overlap. If `N` does not divide the length of the
33133313
/// iterator, then the last up to `N-1` elements will be omitted and can be
3314-
/// retrieved from the [`.into_remainder()`][ArrayChunks::into_remainder]
3314+
/// retrieved from the [`.into_remainder()`][Chunks::into_remainder]
33153315
/// function of the iterator.
33163316
///
33173317
/// # Panics
@@ -3325,7 +3325,7 @@ pub trait Iterator {
33253325
/// ```
33263326
/// #![feature(iter_array_chunks)]
33273327
///
3328-
/// let mut iter = "lorem".chars().array_chunks();
3328+
/// let mut iter = "lorem".chars().chunks();
33293329
/// assert_eq!(iter.next(), Some(['l', 'o']));
33303330
/// assert_eq!(iter.next(), Some(['r', 'e']));
33313331
/// assert_eq!(iter.next(), None);
@@ -3337,17 +3337,17 @@ pub trait Iterator {
33373337
///
33383338
/// let data = [1, 1, 2, -2, 6, 0, 3, 1];
33393339
/// // ^-----^ ^------^
3340-
/// for [x, y, z] in data.iter().array_chunks() {
3340+
/// for [x, y, z] in data.iter().chunks() {
33413341
/// assert_eq!(x + y + z, 4);
33423342
/// }
33433343
/// ```
33443344
#[track_caller]
33453345
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
3346-
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>
3346+
fn chunks<const N: usize>(self) -> Chunks<Self, N>
33473347
where
33483348
Self: Sized,
33493349
{
3350-
ArrayChunks::new(self)
3350+
Chunks::new(self)
33513351
}
33523352

33533353
/// Sums the elements of an iterator.

library/core/tests/iter/adapters/array_chunks.rs renamed to library/core/tests/iter/adapters/chunks.rs

+37-37
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ use core::iter::{self, Iterator};
44
use super::*;
55

66
#[test]
7-
fn test_iterator_array_chunks_infer() {
7+
fn test_iterator_chunks_infer() {
88
let xs = [1, 1, 2, -2, 6, 0, 3, 1];
9-
for [a, b, c] in xs.iter().copied().array_chunks() {
9+
for [a, b, c] in xs.iter().copied().chunks() {
1010
assert_eq!(a + b + c, 4);
1111
}
1212
}
1313

1414
#[test]
15-
fn test_iterator_array_chunks_clone_and_drop() {
15+
fn test_iterator_chunks_clone_and_drop() {
1616
let count = Cell::new(0);
17-
let mut it = (0..5).map(|_| CountDrop::new(&count)).array_chunks::<3>();
17+
let mut it = (0..5).map(|_| CountDrop::new(&count)).chunks::<3>();
1818
assert_eq!(it.by_ref().count(), 1);
1919
assert_eq!(count.get(), 3);
2020
let mut it2 = it.clone();
@@ -27,62 +27,62 @@ fn test_iterator_array_chunks_clone_and_drop() {
2727
}
2828

2929
#[test]
30-
fn test_iterator_array_chunks_remainder() {
31-
let mut it = (0..11).array_chunks::<4>();
30+
fn test_iterator_chunks_remainder() {
31+
let mut it = (0..11).chunks::<4>();
3232
assert_eq!(it.next(), Some([0, 1, 2, 3]));
3333
assert_eq!(it.next(), Some([4, 5, 6, 7]));
3434
assert_eq!(it.next(), None);
3535
assert_eq!(it.into_remainder().unwrap().as_slice(), &[8, 9, 10]);
3636
}
3737

3838
#[test]
39-
fn test_iterator_array_chunks_size_hint() {
40-
let it = (0..6).array_chunks::<1>();
39+
fn test_iterator_chunks_size_hint() {
40+
let it = (0..6).chunks::<1>();
4141
assert_eq!(it.size_hint(), (6, Some(6)));
4242

43-
let it = (0..6).array_chunks::<3>();
43+
let it = (0..6).chunks::<3>();
4444
assert_eq!(it.size_hint(), (2, Some(2)));
4545

46-
let it = (0..6).array_chunks::<5>();
46+
let it = (0..6).chunks::<5>();
4747
assert_eq!(it.size_hint(), (1, Some(1)));
4848

49-
let it = (0..6).array_chunks::<7>();
49+
let it = (0..6).chunks::<7>();
5050
assert_eq!(it.size_hint(), (0, Some(0)));
5151

52-
let it = (1..).array_chunks::<2>();
52+
let it = (1..).chunks::<2>();
5353
assert_eq!(it.size_hint(), (usize::MAX / 2, None));
5454

55-
let it = (1..).filter(|x| x % 2 != 0).array_chunks::<2>();
55+
let it = (1..).filter(|x| x % 2 != 0).chunks::<2>();
5656
assert_eq!(it.size_hint(), (0, None));
5757
}
5858

5959
#[test]
60-
fn test_iterator_array_chunks_count() {
61-
let it = (0..6).array_chunks::<1>();
60+
fn test_iterator_chunks_count() {
61+
let it = (0..6).chunks::<1>();
6262
assert_eq!(it.count(), 6);
6363

64-
let it = (0..6).array_chunks::<3>();
64+
let it = (0..6).chunks::<3>();
6565
assert_eq!(it.count(), 2);
6666

67-
let it = (0..6).array_chunks::<5>();
67+
let it = (0..6).chunks::<5>();
6868
assert_eq!(it.count(), 1);
6969

70-
let it = (0..6).array_chunks::<7>();
70+
let it = (0..6).chunks::<7>();
7171
assert_eq!(it.count(), 0);
7272

73-
let it = (0..6).filter(|x| x % 2 == 0).array_chunks::<2>();
73+
let it = (0..6).filter(|x| x % 2 == 0).chunks::<2>();
7474
assert_eq!(it.count(), 1);
7575

76-
let it = iter::empty::<i32>().array_chunks::<2>();
76+
let it = iter::empty::<i32>().chunks::<2>();
7777
assert_eq!(it.count(), 0);
7878

79-
let it = [(); usize::MAX].iter().array_chunks::<2>();
79+
let it = [(); usize::MAX].iter().chunks::<2>();
8080
assert_eq!(it.count(), usize::MAX / 2);
8181
}
8282

8383
#[test]
84-
fn test_iterator_array_chunks_next_and_next_back() {
85-
let mut it = (0..11).array_chunks::<3>();
84+
fn test_iterator_chunks_next_and_next_back() {
85+
let mut it = (0..11).chunks::<3>();
8686
assert_eq!(it.next(), Some([0, 1, 2]));
8787
assert_eq!(it.next_back(), Some([6, 7, 8]));
8888
assert_eq!(it.next(), Some([3, 4, 5]));
@@ -94,8 +94,8 @@ fn test_iterator_array_chunks_next_and_next_back() {
9494
}
9595

9696
#[test]
97-
fn test_iterator_array_chunks_rev_remainder() {
98-
let mut it = (0..11).array_chunks::<4>();
97+
fn test_iterator_chunks_rev_remainder() {
98+
let mut it = (0..11).chunks::<4>();
9999
{
100100
let mut it = it.by_ref().rev();
101101
assert_eq!(it.next(), Some([4, 5, 6, 7]));
@@ -107,17 +107,17 @@ fn test_iterator_array_chunks_rev_remainder() {
107107
}
108108

109109
#[test]
110-
fn test_iterator_array_chunks_try_fold() {
110+
fn test_iterator_chunks_try_fold() {
111111
let count = Cell::new(0);
112-
let mut it = (0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>();
112+
let mut it = (0..10).map(|_| CountDrop::new(&count)).chunks::<3>();
113113
let result: Result<_, ()> = it.by_ref().try_fold(0, |acc, _item| Ok(acc + 1));
114114
assert_eq!(result, Ok(3));
115115
assert_eq!(count.get(), 9);
116116
drop(it);
117117
assert_eq!(count.get(), 10);
118118

119119
let count = Cell::new(0);
120-
let mut it = (0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>();
120+
let mut it = (0..10).map(|_| CountDrop::new(&count)).chunks::<3>();
121121
let result = it.by_ref().try_fold(0, |acc, _item| if acc < 2 { Ok(acc + 1) } else { Err(acc) });
122122
assert_eq!(result, Err(2));
123123
assert_eq!(count.get(), 9);
@@ -126,8 +126,8 @@ fn test_iterator_array_chunks_try_fold() {
126126
}
127127

128128
#[test]
129-
fn test_iterator_array_chunks_fold() {
130-
let result = (1..11).array_chunks::<3>().fold(0, |acc, [a, b, c]| {
129+
fn test_iterator_chunks_fold() {
130+
let result = (1..11).chunks::<3>().fold(0, |acc, [a, b, c]| {
131131
assert_eq!(acc + 1, a);
132132
assert_eq!(acc + 2, b);
133133
assert_eq!(acc + 3, c);
@@ -137,24 +137,24 @@ fn test_iterator_array_chunks_fold() {
137137

138138
let count = Cell::new(0);
139139
let result =
140-
(0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>().fold(0, |acc, _item| acc + 1);
140+
(0..10).map(|_| CountDrop::new(&count)).chunks::<3>().fold(0, |acc, _item| acc + 1);
141141
assert_eq!(result, 3);
142142
// fold impls may or may not process the remainder
143143
assert!(count.get() <= 10 && count.get() >= 9);
144144
}
145145

146146
#[test]
147-
fn test_iterator_array_chunks_try_rfold() {
147+
fn test_iterator_chunks_try_rfold() {
148148
let count = Cell::new(0);
149-
let mut it = (0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>();
149+
let mut it = (0..10).map(|_| CountDrop::new(&count)).chunks::<3>();
150150
let result: Result<_, ()> = it.try_rfold(0, |acc, _item| Ok(acc + 1));
151151
assert_eq!(result, Ok(3));
152152
assert_eq!(count.get(), 9);
153153
drop(it);
154154
assert_eq!(count.get(), 10);
155155

156156
let count = Cell::new(0);
157-
let mut it = (0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>();
157+
let mut it = (0..10).map(|_| CountDrop::new(&count)).chunks::<3>();
158158
let result = it.try_rfold(0, |acc, _item| if acc < 2 { Ok(acc + 1) } else { Err(acc) });
159159
assert_eq!(result, Err(2));
160160
assert_eq!(count.get(), 9);
@@ -163,8 +163,8 @@ fn test_iterator_array_chunks_try_rfold() {
163163
}
164164

165165
#[test]
166-
fn test_iterator_array_chunks_rfold() {
167-
let result = (1..11).array_chunks::<3>().rfold(0, |acc, [a, b, c]| {
166+
fn test_iterator_chunks_rfold() {
167+
let result = (1..11).chunks::<3>().rfold(0, |acc, [a, b, c]| {
168168
assert_eq!(10 - (acc + 1), c);
169169
assert_eq!(10 - (acc + 2), b);
170170
assert_eq!(10 - (acc + 3), a);
@@ -174,7 +174,7 @@ fn test_iterator_array_chunks_rfold() {
174174

175175
let count = Cell::new(0);
176176
let result =
177-
(0..10).map(|_| CountDrop::new(&count)).array_chunks::<3>().rfold(0, |acc, _item| acc + 1);
177+
(0..10).map(|_| CountDrop::new(&count)).chunks::<3>().rfold(0, |acc, _item| acc + 1);
178178
assert_eq!(result, 3);
179179
assert_eq!(count.get(), 10);
180180
}

library/core/tests/iter/adapters/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
mod array_chunks;
21
mod by_ref_sized;
32
mod chain;
3+
mod chunks;
44
mod cloned;
55
mod copied;
66
mod cycle;

0 commit comments

Comments
 (0)