@@ -1687,6 +1687,106 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
1687
1687
}
1688
1688
}
1689
1689
1690
+ /// A windowed iterator over a slice in overlapping chunks (`N` elements at a
1691
+ /// time), starting at the beginning of the slice
1692
+ ///
1693
+ /// This struct is created by the [`array_windows`] method on [slices].
1694
+ ///
1695
+ /// [`array_windows`]: ../../std/primitive.slice.html#method.array_windows
1696
+ /// [slices]: ../../std/primitive.slice.html
1697
+ #[ derive( Debug , Clone , Copy ) ]
1698
+ #[ unstable( feature = "array_windows" , issue = "75027" ) ]
1699
+ pub struct ArrayWindows < ' a , T : ' a , const N : usize > {
1700
+ pub ( crate ) slice_head : * const T ,
1701
+ pub ( crate ) num : usize ,
1702
+ pub ( crate ) marker : marker:: PhantomData < & ' a [ T ; N ] > ,
1703
+ }
1704
+
1705
+ #[ unstable( feature = "array_windows" , issue = "75027" ) ]
1706
+ impl < ' a , T , const N : usize > Iterator for ArrayWindows < ' a , T , N > {
1707
+ type Item = & ' a [ T ; N ] ;
1708
+
1709
+ #[ inline]
1710
+ fn next ( & mut self ) -> Option < Self :: Item > {
1711
+ if self . num == 0 {
1712
+ return None ;
1713
+ }
1714
+ // SAFETY:
1715
+ // This is safe because it's indexing into a slice guaranteed to be length > N.
1716
+ let ret = unsafe { & * self . slice_head . cast :: < [ T ; N ] > ( ) } ;
1717
+ // SAFETY: Guaranteed that there are at least 1 item remaining otherwise
1718
+ // earlier branch would've been hit
1719
+ self . slice_head = unsafe { self . slice_head . add ( 1 ) } ;
1720
+
1721
+ self . num -= 1 ;
1722
+ Some ( ret)
1723
+ }
1724
+
1725
+ #[ inline]
1726
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1727
+ ( self . num , Some ( self . num ) )
1728
+ }
1729
+
1730
+ #[ inline]
1731
+ fn count ( self ) -> usize {
1732
+ self . num
1733
+ }
1734
+
1735
+ #[ inline]
1736
+ fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
1737
+ if self . num <= n {
1738
+ self . num = 0 ;
1739
+ return None ;
1740
+ }
1741
+ // SAFETY:
1742
+ // This is safe because it's indexing into a slice guaranteed to be length > N.
1743
+ let ret = unsafe { & * self . slice_head . add ( n) . cast :: < [ T ; N ] > ( ) } ;
1744
+ // SAFETY: Guaranteed that there are at least n items remaining
1745
+ self . slice_head = unsafe { self . slice_head . add ( n + 1 ) } ;
1746
+
1747
+ self . num -= n + 1 ;
1748
+ Some ( ret)
1749
+ }
1750
+
1751
+ #[ inline]
1752
+ fn last ( mut self ) -> Option < Self :: Item > {
1753
+ self . nth ( self . num . checked_sub ( 1 ) ?)
1754
+ }
1755
+ }
1756
+
1757
+ #[ unstable( feature = "array_windows" , issue = "75027" ) ]
1758
+ impl < ' a , T , const N : usize > DoubleEndedIterator for ArrayWindows < ' a , T , N > {
1759
+ #[ inline]
1760
+ fn next_back ( & mut self ) -> Option < & ' a [ T ; N ] > {
1761
+ if self . num == 0 {
1762
+ return None ;
1763
+ }
1764
+ // SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
1765
+ let ret = unsafe { & * self . slice_head . add ( self . num - 1 ) . cast :: < [ T ; N ] > ( ) } ;
1766
+ self . num -= 1 ;
1767
+ Some ( ret)
1768
+ }
1769
+
1770
+ #[ inline]
1771
+ fn nth_back ( & mut self , n : usize ) -> Option < & ' a [ T ; N ] > {
1772
+ if self . num <= n {
1773
+ self . num = 0 ;
1774
+ return None ;
1775
+ }
1776
+ // SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
1777
+ let ret = unsafe { & * self . slice_head . add ( self . num - ( n + 1 ) ) . cast :: < [ T ; N ] > ( ) } ;
1778
+ self . num -= n + 1 ;
1779
+ Some ( ret)
1780
+ }
1781
+ }
1782
+
1783
+ #[ unstable( feature = "array_windows" , issue = "75027" ) ]
1784
+ impl < T , const N : usize > ExactSizeIterator for ArrayWindows < ' _ , T , N > {
1785
+ fn is_empty ( & self ) -> bool {
1786
+ self . num == 0
1787
+ }
1788
+ }
1789
+
1690
1790
/// An iterator over a slice in (non-overlapping) chunks (`N` elements at a
1691
1791
/// time), starting at the beginning of the slice.
1692
1792
///
0 commit comments