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

Tracking Issue for const_slice_make_iter #137737

Open
3 tasks
Daniel-Aaron-Bloom opened this issue Feb 27, 2025 · 1 comment
Open
3 tasks

Tracking Issue for const_slice_make_iter #137737

Daniel-Aaron-Bloom opened this issue Feb 27, 2025 · 1 comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Daniel-Aaron-Bloom
Copy link

Daniel-Aaron-Bloom commented Feb 27, 2025

Feature gate: #![feature(const_slice_make_iter)]

This is a tracking issue for making the following methods const:

This enables the storing iterators in associated constants, which can be marginally helpful for some esoteric use cases:

trait IterableState {
    type Iterator: Iterator<Item=&'static u8>;
    const ITERATOR: Self::Iterator;
}
struct Basic;
impl IterableState for Basic {
    type Iterator = Iter<u8>;
    const ITERATOR: Self::Iterator = [3, 1, 4, 1, 5].iter();
}

struct Filtered<I>(PhantomData<I>);
impl<I: IterableState> IterableState for Filtered<I> {
    // Unfortunately still no way to `const`ruct `std::iter::Filter`, but you can
    // copy the code and make `Filter::new` const
    type Iterator = MyFilter<u8>; 
    const ITERATOR: Self::Iterator = MyFilter::new(I::ITERATOR, filter_fn);
}

fn filter_fn(v: &&u8) -> bool {
    **v > 100
}

Now, you might be saying, that seems extremely niche. And you'd be right. But with const getting stronger and stronger with each release, having unnecessary const limitations is preventing code reuse between const and non-const functions.

Public API

// std::slice
impl<T> [T] {
    pub const fn iter(&self) -> Iter<'_, T> { ... }
    pub const fn iter_mut(&mut self) -> IterMut<'_, T> { ... }
    pub const fn windows(&self, size: usize) -> Windows<'_, T> { ... }
    pub const fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> { ... }
    pub const fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> { ... }
    pub const fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> { ... }
    pub const fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { ... }
    pub const fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N>{ ... }
    pub const fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> { ... }
    pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> { ... }
    pub const fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> { ... }
    pub const fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> { ... }
    pub const fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> { ... }
    pub const fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> { ... }
    pub const fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
    where F: FnMut(&T, &T) -> bool, { ... }
    pub const fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
    where F: FnMut(&T, &T) -> bool{ ... }
}

Steps

Unresolved Questions

  • None yet.

Footnotes

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@Daniel-Aaron-Bloom Daniel-Aaron-Bloom added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label Feb 27, 2025
@Daniel-Aaron-Bloom
Copy link
Author

@rustbot label +T-libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants