Tracking Issue for const_slice_make_iter
· Issue #137737 · rust-lang/rust (original) (raw)
Feature gate: #![feature(const_slice_make_iter)]
This is a tracking issue for making the following methods const
:
- slice::iter
- slice::iter_mut
- slice::windows
- slice::chunks
- slice::chunks_mut
- slice::chunks_exact
- slice::chunks_exact_mut
- slice::array_chunks
- slice::array_chunks_mut
- slice::array_windows
- slice::rchunks
- slice::rchunks_mut
- slice::rchunks_exact
- slice::rchunks_exact_mut
- slice::chunk_by
- slice::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; const ITERATOR: Self::Iterator = [3, 1, 4, 1, 5].iter(); }
struct Filtered(PhantomData);
impl<I: IterableState> IterableState for Filtered {
// Unfortunately still no way to const
ruct std::iter::Filter
, but you can
// copy the code and make Filter::new
const
type Iterator = MyFilter;
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] { 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(&self) -> ArrayChunks<'_, T, N>{ ... } pub const fn array_chunks_mut(&mut self) -> ArrayChunksMut<'_, T, N> { ... } pub const fn array_windows(&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(&self, pred: F) -> ChunkBy<'_, T, F> where F: FnMut(&T, &T) -> bool, { ... } pub const fn chunk_by_mut(&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.