rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.64k stars 299 forks source link

`map_windows` without const-generics? #893

Closed Philippe-Cholet closed 2 months ago

Philippe-Cholet commented 4 months ago

As of today, Iterator::map_windows is nightly:

fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
where
    Self: Sized,
    F: FnMut(&[Self::Item; N]) -> R;

But I'm wondering if we should have a version of it without const-generics (so a boxed slice internally) as it would allow to have the feature with a n unknown at compile time, similar to slice.windows(n).map(f).

#[cfg(feature = "use_alloc")]
fn map_boxed_windows<F, R>(self, n: usize, f: F) -> MapBoxedWindows<Self, F>    // or any other names
where
    Self: Sized,
    F: FnMut(&[Self::Item]) -> R;

Both allocate once. The first one would be my choice but the second would be simpler and easier to review.

We could also wait for lending iterators to discard F entirely, but it's gonna be a long wait.