]> git.proxmox.com Git - rustc.git/blob - tests/ui/const-generics/type-dependent/issue-61936.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / const-generics / type-dependent / issue-61936.rs
1 // run-pass
2
3 trait SliceExt<T: Clone> {
4 fn array_windows_example<'a, const N: usize>(&'a self) -> ArrayWindowsExample<'a, T, N>;
5 }
6
7 impl <T: Clone> SliceExt<T> for [T] {
8 fn array_windows_example<'a, const N: usize>(&'a self) -> ArrayWindowsExample<'a, T, N> {
9 ArrayWindowsExample{ idx: 0, slice: &self }
10 }
11 }
12
13 struct ArrayWindowsExample<'a, T, const N: usize> {
14 slice: &'a [T],
15 idx: usize,
16 }
17
18 impl <'a, T: Clone, const N: usize> Iterator for ArrayWindowsExample<'a, T, N> {
19 type Item = [T; N];
20 fn next(&mut self) -> Option<Self::Item> {
21 // Note: this is unsound for some `T` and not meant as an example
22 // on how to implement `ArrayWindows`.
23 let mut res = unsafe{ std::mem::zeroed() };
24 let mut ptr = &mut res as *mut [T; N] as *mut T;
25
26 for i in 0..N {
27 match self.slice[self.idx..].get(i) {
28 None => return None,
29 Some(elem) => unsafe { std::ptr::write_volatile(ptr, elem.clone())},
30 };
31 ptr = ptr.wrapping_add(1);
32 self.idx += 1;
33 }
34
35 Some(res)
36 }
37 }
38
39 const FOUR: usize = 4;
40
41 fn main() {
42 let v: Vec<usize> = vec![0; 100];
43
44 for array in v.as_slice().array_windows_example::<FOUR>() {
45 assert_eq!(array, [0, 0, 0, 0])
46 }
47 }