2 #![feature(const_generics)]
3 #![allow(incomplete_features)]
5 trait ConstChunksExactTrait<T> {
6 fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}>;
9 impl <T> ConstChunksExactTrait<T> for [T] {
10 fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}> {
12 let rem = self.len() % N;
13 let len = self.len() - rem;
14 let (fst, _) = self.split_at(len);
15 ConstChunksExact { v: fst, }
19 struct ConstChunksExact<'a, T: 'a, const N: usize> {
23 impl <'a, T: std::fmt::Debug, const N: usize> Iterator for ConstChunksExact<'a, T, {N}> {
24 type Item = &'a [T; N];
26 fn next(&mut self) -> Option<Self::Item> {
30 let (fst, snd) = self.v.split_at(N);
33 let ptr = fst.as_ptr() as *const _;
40 let slice = &[1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10];
42 let mut iter = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].iter();
44 for a in slice.const_chunks_exact::<3>() {
45 assert_eq!(a, iter.next().unwrap());