3 trait SliceExt<T: Clone> {
4 fn array_windows_example<'a, const N: usize>(&'a self) -> ArrayWindowsExample<'a, T, N>;
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 }
13 struct ArrayWindowsExample<'a, T, const N: usize> {
18 impl <'a, T: Clone, const N: usize> Iterator for ArrayWindowsExample<'a, 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;
27 match self.slice[self.idx..].get(i) {
29 Some(elem) => unsafe { std::ptr::write_volatile(ptr, elem.clone())},
31 ptr = ptr.wrapping_add(1);
39 const FOUR: usize = 4;
42 let v: Vec<usize> = vec![0; 100];
44 for array in v.as_slice().array_windows_example::<FOUR>() {
45 assert_eq!(array, [0, 0, 0, 0])