Skip to content

Commit

Permalink
arrays: Rename next_chunk* to from_iter*
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Dec 15, 2023
1 parent 7623f76 commit d63e0a7
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions crates/arrays/src/lib.rs
Expand Up @@ -12,7 +12,7 @@
//!
//! ```
//! # let iter = 1..5;
//! let arr: [_; 3] = arrays::next_chunk(iter).unwrap();
//! let arr: [_; 3] = arrays::from_iter(iter).unwrap();
//! ```

#![no_std]
Expand All @@ -38,7 +38,7 @@ pub use crate::into_iter::IntoIter;
///
/// If the iterator panics then all already yielded elements will be dropped.
#[inline]
pub fn next_chunk<I, T, const N: usize>(mut iter: I) -> Result<[T; N], IntoIter<T, N>>
pub fn from_iter<I, T, const N: usize>(mut iter: I) -> Result<[T; N], IntoIter<T, N>>
where
I: Iterator<Item = T>,
{
Expand Down Expand Up @@ -104,18 +104,18 @@ where
///
/// # Safety
///
/// This function is the same as [`next_chunk`] but the caller must guarantee
/// This function is the same as [`from_iter`] but the caller must guarantee
/// that the iterator yields at least N items or panic.
///
/// # Panics
///
/// If the iterator panics then all already yielded elements will be dropped.
#[inline]
pub unsafe fn next_chunk_unchecked<I, T, const N: usize>(iter: I) -> [T; N]
pub unsafe fn from_iter_unchecked<I, T, const N: usize>(iter: I) -> [T; N]
where
I: Iterator<Item = T>,
{
match next_chunk(iter) {
match from_iter(iter) {
Ok(arr) => arr,
Err(_) =>
// SAFETY: Guaranteed by the caller.
Expand Down
6 changes: 3 additions & 3 deletions crates/arrays/tests/lib.rs
Expand Up @@ -5,7 +5,7 @@ use std::sync::atomic::Ordering;
#[test]
fn next_chunk_fewer() {
let iter = [1, 2, 3].into_iter();
let res: Result<[i32; 4], _> = arrays::next_chunk(iter);
let res: Result<[i32; 4], _> = arrays::from_iter(iter);
let mut rem = res.unwrap_err();
assert_eq!(rem.next(), Some(1));
assert_eq!(rem.next(), Some(2));
Expand Down Expand Up @@ -33,7 +33,7 @@ fn next_chunk_panic() {
});

let res = panic::catch_unwind(|| {
let _: [Foo; 3] = arrays::next_chunk(iter).unwrap();
let _: [Foo; 3] = arrays::from_iter(iter).unwrap();
});
assert!(res.is_err());
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
Expand All @@ -60,7 +60,7 @@ fn next_chunk_unchecked_panic() {
});

let res = panic::catch_unwind(|| {
let _: [Foo; 3] = unsafe { arrays::next_chunk_unchecked(iter) };
let _: [Foo; 3] = unsafe { arrays::from_iter_unchecked(iter) };
});
assert!(res.is_err());
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
Expand Down
6 changes: 3 additions & 3 deletions src/adaptors/array_chunks.rs
Expand Up @@ -111,7 +111,7 @@ where
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let Self { iter, remainder } = self;
match arrays::next_chunk(iter) {
match arrays::from_iter(iter) {
Ok(chunk) => Some(chunk),
Err(rem) => {
remainder.get_or_insert(rem);
Expand Down Expand Up @@ -140,7 +140,7 @@ where
fn next_back(&mut self) -> Option<Self::Item> {
self.next_back_remainder();
let mut rev = self.iter.by_ref().rev();
let mut chunk = arrays::next_chunk(&mut rev).ok()?;
let mut chunk = arrays::from_iter(&mut rev).ok()?;
chunk.reverse();
Some(chunk)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ where
let rem = self.iter.len() % N;
let mut rev = self.iter.by_ref().rev().take(rem);
// SAFETY: `unwrap_err` always succeeds because x % N < N for all x.
let mut remainder = unsafe { arrays::next_chunk(&mut rev).unwrap_err_unchecked() };
let mut remainder = unsafe { arrays::from_iter(&mut rev).unwrap_err_unchecked() };

// We used `.rev()` above, so we need to re-reverse the remainder.
remainder.as_mut_slice().reverse();
Expand Down
4 changes: 2 additions & 2 deletions src/adaptors/array_combinations.rs
Expand Up @@ -69,7 +69,7 @@ where
assert!(K != 0, "combination size must be non-zero");

// SAFETY: The range 0..K yields at least K elements.
let comb = unsafe { arrays::next_chunk_unchecked(0..K) };
let comb = unsafe { arrays::from_iter_unchecked(0..K) };

Self(GenericCombinations::new(iter, comb))
}
Expand Down Expand Up @@ -106,7 +106,7 @@ where
self.0.fill_next().map(|it| {
// SAFETY: The iterator is guaranteed to yield K elements because
// it is derived from `self.0.comb` which is an array of length K.
unsafe { arrays::next_chunk_unchecked(it) }
unsafe { arrays::from_iter_unchecked(it) }
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/array_combinations_with_reps.rs
Expand Up @@ -102,7 +102,7 @@ where
self.0.fill_next_with_reps().map(|it| {
// SAFETY: The iterator is guaranteed to yield K elements because
// it is derived from `self.0.comb` which is an array of length K.
unsafe { arrays::next_chunk_unchecked(it) }
unsafe { arrays::from_iter_unchecked(it) }
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/array_windows.rs
Expand Up @@ -101,7 +101,7 @@ where
Some(last.clone())
}
None => {
let tmp = arrays::next_chunk(iter).ok()?;
let tmp = arrays::from_iter(iter).ok()?;
*last = Some(tmp.clone());
Some(tmp)
}
Expand Down
4 changes: 2 additions & 2 deletions src/xtraits/next_chunk.rs
Expand Up @@ -45,7 +45,7 @@ pub trait IterNextChunk: Iterator {
where
Self: Sized,
{
arrays::next_chunk(self)
arrays::from_iter(self)
}

/// Identical to [`next_chunk`][IterNextChunk::next_chunk] but doesn't
Expand All @@ -55,7 +55,7 @@ pub trait IterNextChunk: Iterator {
where
Self: Sized,
{
arrays::next_chunk(self)
arrays::from_iter(self)
}
}

Expand Down

0 comments on commit d63e0a7

Please sign in to comment.