Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MapWindows compile fail check for N=0 #121156

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion library/core/src/iter/adapters/map_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ struct Buffer<T, const N: usize> {
}

impl<I: Iterator, F, const N: usize> MapWindows<I, F, N> {
pub(in crate::iter) fn new(iter: I, f: F) -> Self {
const N_NOT_ZERO: () =
assert!(N != 0, "array in `Iterator::map_windows` must contain more than 0 elements");

pub(in crate::iter) fn new(iter: I, f: F) -> Self {
let _ = Self::N_NOT_ZERO;

// Only ZST arrays' length can be so large.
if mem::size_of::<I::Item>() == 0 {
assert!(
Expand Down
13 changes: 8 additions & 5 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,17 +1609,20 @@ pub trait Iterator {
/// [`slice::windows()`]: slice::windows
/// [`FusedIterator`]: crate::iter::FusedIterator
///
/// # Panics
///
/// Panics if `N` is 0. This check will most probably get changed to a
/// compile time error before this method gets stabilized.
/// If `N` is 0, then the code will not compile.
///
/// ```should_panic
/// ```compile_fail
/// #![feature(iter_map_windows)]
///
/// let iter = std::iter::repeat(0).map_windows(|&[]| ());
/// ```
///
/// ```compile_fail
/// #![feature(iter_map_windows)]
///
/// let iter = std::iter::repeat(0).map_windows(|_: &[_; 0]| ());
/// ```
///
/// # Examples
///
/// Building the sums of neighboring numbers.
Expand Down
6 changes: 0 additions & 6 deletions library/core/tests/iter/adapters/map_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ fn test_case_from_pr_82413_comment() {
for () in std::iter::repeat("0".to_owned()).map_windows(|_: &[_; 3]| {}).take(4) {}
}

#[test]
#[should_panic = "array in `Iterator::map_windows` must contain more than 0 elements"]
fn check_zero_window() {
let _ = std::iter::repeat(0).map_windows(|_: &[_; 0]| ());
}

#[test]
fn test_zero_sized_type() {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down
Loading