@@ -2203,16 +2203,13 @@ unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
22032203#[ unstable( feature = "array_windows" , issue = "75027" ) ]
22042204#[ must_use = "iterators are lazy and do nothing unless consumed" ]
22052205pub struct ArrayWindows < ' a , T : ' a , const N : usize > {
2206- slice_head : * const T ,
2207- num : usize ,
2208- marker : PhantomData < & ' a [ T ; N ] > ,
2206+ v : & ' a [ T ] ,
22092207}
22102208
22112209impl < ' a , T : ' a , const N : usize > ArrayWindows < ' a , T , N > {
22122210 #[ inline]
22132211 pub ( super ) const fn new ( slice : & ' a [ T ] ) -> Self {
2214- let num_windows = slice. len ( ) . saturating_sub ( N - 1 ) ;
2215- Self { slice_head : slice. as_ptr ( ) , num : num_windows, marker : PhantomData }
2212+ Self { v : slice }
22162213 }
22172214}
22182215
@@ -2222,82 +2219,68 @@ impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
22222219
22232220 #[ inline]
22242221 fn next ( & mut self ) -> Option < Self :: Item > {
2225- if self . num == 0 {
2226- return None ;
2222+ if let Some ( ret) = self . v . first_chunk ( ) {
2223+ self . v = & self . v [ 1 ..] ;
2224+ Some ( ret)
2225+ } else {
2226+ None
22272227 }
2228- // SAFETY:
2229- // This is safe because it's indexing into a slice guaranteed to be length > N.
2230- let ret = unsafe { & * self . slice_head . cast :: < [ T ; N ] > ( ) } ;
2231- // SAFETY: Guaranteed that there are at least 1 item remaining otherwise
2232- // earlier branch would've been hit
2233- self . slice_head = unsafe { self . slice_head . add ( 1 ) } ;
2234-
2235- self . num -= 1 ;
2236- Some ( ret)
22372228 }
22382229
22392230 #[ inline]
22402231 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2241- ( self . num , Some ( self . num ) )
2232+ if self . v . len ( ) < N {
2233+ ( 0 , Some ( 0 ) )
2234+ } else {
2235+ let size = self . v . len ( ) - N + 1 ;
2236+ ( size, Some ( size) )
2237+ }
22422238 }
22432239
22442240 #[ inline]
22452241 fn count ( self ) -> usize {
2246- self . num
2242+ self . size_hint ( ) . 0
22472243 }
22482244
22492245 #[ inline]
22502246 fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
2251- if self . num <= n {
2252- self . num = 0 ;
2253- return None ;
2254- }
2255- // SAFETY:
2256- // This is safe because it's indexing into a slice guaranteed to be length > N.
2257- let ret = unsafe { & * self . slice_head . add ( n) . cast :: < [ T ; N ] > ( ) } ;
2258- // SAFETY: Guaranteed that there are at least n items remaining
2259- self . slice_head = unsafe { self . slice_head . add ( n + 1 ) } ;
2260-
2261- self . num -= n + 1 ;
2262- Some ( ret)
2247+ if self . v . split_off ( ..n) . is_some ( ) { self . next ( ) } else { None }
22632248 }
22642249
22652250 #[ inline]
2266- fn last ( mut self ) -> Option < Self :: Item > {
2267- self . nth ( self . num . checked_sub ( 1 ) ? )
2251+ fn last ( self ) -> Option < Self :: Item > {
2252+ self . v . last_chunk ( )
22682253 }
22692254}
22702255
22712256#[ unstable( feature = "array_windows" , issue = "75027" ) ]
22722257impl < ' a , T , const N : usize > DoubleEndedIterator for ArrayWindows < ' a , T , N > {
22732258 #[ inline]
22742259 fn next_back ( & mut self ) -> Option < & ' a [ T ; N ] > {
2275- if self . num == 0 {
2276- return None ;
2260+ if let Some ( ret) = self . v . last_chunk ( ) {
2261+ self . v = & self . v [ ..self . v . len ( ) - 1 ] ;
2262+ Some ( ret)
2263+ } else {
2264+ None
22772265 }
2278- // SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
2279- let ret = unsafe { & * self . slice_head . add ( self . num - 1 ) . cast :: < [ T ; N ] > ( ) } ;
2280- self . num -= 1 ;
2281- Some ( ret)
22822266 }
22832267
22842268 #[ inline]
22852269 fn nth_back ( & mut self , n : usize ) -> Option < & ' a [ T ; N ] > {
2286- if self . num <= n {
2287- self . num = 0 ;
2288- return None ;
2270+ if let Some ( idx) = self . v . len ( ) . checked_sub ( n)
2271+ && self . v . split_off ( idx..) . is_some ( )
2272+ {
2273+ self . next_back ( )
2274+ } else {
2275+ None
22892276 }
2290- // SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
2291- let ret = unsafe { & * self . slice_head . add ( self . num - ( n + 1 ) ) . cast :: < [ T ; N ] > ( ) } ;
2292- self . num -= n + 1 ;
2293- Some ( ret)
22942277 }
22952278}
22962279
22972280#[ unstable( feature = "array_windows" , issue = "75027" ) ]
22982281impl < T , const N : usize > ExactSizeIterator for ArrayWindows < ' _ , T , N > {
22992282 fn is_empty ( & self ) -> bool {
2300- self . num == 0
2283+ self . v . len ( ) < N
23012284 }
23022285}
23032286
0 commit comments