-
Notifications
You must be signed in to change notification settings - Fork 14k
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.Status: The implementation is "complete" but it needs time to bake.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(const_slice_make_iter)]
This is a tracking issue for making the following methods const:
slice::iterslice::iter_mutslice::windowsslice::chunksslice::chunks_mutslice::chunks_exactslice::chunks_exact_mutslice::array_windowsslice::rchunksslice::rchunks_mutslice::rchunks_exactslice::rchunks_exact_mutslice::chunk_byslice::chunk_by_mut
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: Make slice iterator constructors unstably const #137738
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- None yet.
Footnotes
Footnotes
anqur, Cheban1996 and Filiprogrammer
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.Status: The implementation is "complete" but it needs time to bake.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.