]> git.lizzy.rs Git - rust.git/blob - src/test/ui/const-generics/type-dependent/issue-70507.rs
Auto merge of #75028 - MrModder:master, r=steveklabnik
[rust.git] / src / test / ui / const-generics / type-dependent / issue-70507.rs
1 // run-pass
2 #![feature(const_generics)]
3 #![allow(incomplete_features)]
4
5 trait ConstChunksExactTrait<T> {
6     fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}>;
7 }
8
9 impl <T> ConstChunksExactTrait<T> for [T] {
10     fn const_chunks_exact<const N: usize>(&self) -> ConstChunksExact<'_, T, {N}> {
11         assert!(N != 0);
12         let rem = self.len() % N;
13         let len = self.len() - rem;
14         let (fst, _) = self.split_at(len);
15         ConstChunksExact { v: fst, }
16     }
17 }
18
19 struct ConstChunksExact<'a, T: 'a, const N: usize> {
20     v: &'a [T],
21 }
22
23 impl <'a, T: std::fmt::Debug, const N: usize> Iterator for ConstChunksExact<'a, T, {N}> {
24     type Item = &'a [T; N];
25
26     fn next(&mut self) -> Option<Self::Item> {
27         if self.v.len() < N {
28             None
29         } else {
30             let (fst, snd) = self.v.split_at(N);
31
32             self.v = snd;
33             let ptr = fst.as_ptr() as *const _;
34             Some(unsafe { &*ptr})
35         }
36     }
37 }
38
39 fn main() {
40     let slice = &[1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10];
41
42     let mut iter = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].iter();
43
44     for a in slice.const_chunks_exact::<3>() {
45         assert_eq!(a, iter.next().unwrap());
46     }
47 }