Skip to content

Commit

Permalink
Add const assert to MapWindows
Browse files Browse the repository at this point in the history
Checks if const generic N is not set to 0, otherwise it prevents
compilation.
  • Loading branch information
Nydauron committed Dec 10, 2023
1 parent 8cd8d31 commit 31ffa40
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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
7 changes: 2 additions & 5 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,12 +1625,9 @@ 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(|&[]| ());
Expand Down

0 comments on commit 31ffa40

Please sign in to comment.