rust-lang / rust-lang/rust

Tracking Issue for `const_slice_make_iter`

Open
#137,737 2 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue S-tracking-needs-to-bake T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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
  • Implementation: #137738
  • Final comment period (FCP)^1
  • Stabilization PR
Unresolved Questions
  • None yet.

Footnotes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the tracking issue's Public API list and the linked implementation reference, #137738, then read the Rust standard library stabilization and final comment period guidance. Done means completing the remaining FCP and stabilization work for the listed slice methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.