From d63e0a78173f4189d7ade750c1be43c09f2905ea Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Fri, 15 Dec 2023 11:05:05 +0200 Subject: [PATCH] arrays: Rename `next_chunk*` to `from_iter*` --- crates/arrays/src/lib.rs | 10 +++++----- crates/arrays/tests/lib.rs | 6 +++--- src/adaptors/array_chunks.rs | 6 +++--- src/adaptors/array_combinations.rs | 4 ++-- src/adaptors/array_combinations_with_reps.rs | 2 +- src/adaptors/array_windows.rs | 2 +- src/xtraits/next_chunk.rs | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/arrays/src/lib.rs b/crates/arrays/src/lib.rs index b4f0646..f564d5b 100644 --- a/crates/arrays/src/lib.rs +++ b/crates/arrays/src/lib.rs @@ -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] @@ -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(mut iter: I) -> Result<[T; N], IntoIter> +pub fn from_iter(mut iter: I) -> Result<[T; N], IntoIter> where I: Iterator, { @@ -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(iter: I) -> [T; N] +pub unsafe fn from_iter_unchecked(iter: I) -> [T; N] where I: Iterator, { - match next_chunk(iter) { + match from_iter(iter) { Ok(arr) => arr, Err(_) => // SAFETY: Guaranteed by the caller. diff --git a/crates/arrays/tests/lib.rs b/crates/arrays/tests/lib.rs index fad26fd..335f689 100644 --- a/crates/arrays/tests/lib.rs +++ b/crates/arrays/tests/lib.rs @@ -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)); @@ -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); @@ -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); diff --git a/src/adaptors/array_chunks.rs b/src/adaptors/array_chunks.rs index c994144..c751cdd 100644 --- a/src/adaptors/array_chunks.rs +++ b/src/adaptors/array_chunks.rs @@ -111,7 +111,7 @@ where #[inline] fn next(&mut self) -> Option { 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); @@ -140,7 +140,7 @@ where fn next_back(&mut self) -> Option { 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) } @@ -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(); diff --git a/src/adaptors/array_combinations.rs b/src/adaptors/array_combinations.rs index 117ea83..0959535 100644 --- a/src/adaptors/array_combinations.rs +++ b/src/adaptors/array_combinations.rs @@ -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)) } @@ -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) } }) } } diff --git a/src/adaptors/array_combinations_with_reps.rs b/src/adaptors/array_combinations_with_reps.rs index 9943feb..49651b6 100644 --- a/src/adaptors/array_combinations_with_reps.rs +++ b/src/adaptors/array_combinations_with_reps.rs @@ -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) } }) } } diff --git a/src/adaptors/array_windows.rs b/src/adaptors/array_windows.rs index 3b0bf67..71ea9ef 100644 --- a/src/adaptors/array_windows.rs +++ b/src/adaptors/array_windows.rs @@ -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) } diff --git a/src/xtraits/next_chunk.rs b/src/xtraits/next_chunk.rs index 4868d80..d077612 100644 --- a/src/xtraits/next_chunk.rs +++ b/src/xtraits/next_chunk.rs @@ -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 @@ -55,7 +55,7 @@ pub trait IterNextChunk: Iterator { where Self: Sized, { - arrays::next_chunk(self) + arrays::from_iter(self) } }