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

map_windows without const-generics? #893

Closed
Philippe-Cholet opened this issue Mar 3, 2024 · 0 comments
Closed

map_windows without const-generics? #893

Philippe-Cholet opened this issue Mar 3, 2024 · 0 comments

Comments

@Philippe-Cholet
Copy link
Member

Philippe-Cholet commented Mar 3, 2024

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;
  • It can have a very similar internal implementation: a boxed slice of length 2 * n to reduce moves (move all windows' elements one time each n elements).
  • It could have a boxed slice of minimal length n (to minimize allocation) at the cost of moving all the windows' elements every time (with .rotate_left(1)).

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.

@Philippe-Cholet Philippe-Cholet closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant