Skip to content

Tracking Issue for const_slice_make_iter #137737

Open
@Daniel-Aaron-Bloom

Description

@Daniel-Aaron-Bloom

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions