@@ -2203,16 +2203,13 @@ unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
2203
2203
#[ unstable( feature = "array_windows" , issue = "75027" ) ]
2204
2204
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
2205
2205
pub 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 ] ,
2209
2207
}
2210
2208
2211
2209
impl < ' a , T : ' a , const N : usize > ArrayWindows < ' a , T , N > {
2212
2210
#[ inline]
2213
2211
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 }
2216
2213
}
2217
2214
}
2218
2215
@@ -2222,82 +2219,60 @@ impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
2222
2219
2223
2220
#[ inline]
2224
2221
fn next ( & mut self ) -> Option < Self :: Item > {
2225
- if self . num == 0 {
2226
- return None ;
2222
+ let ret = self . v . first_chunk ( ) ;
2223
+ if ret. is_some ( ) {
2224
+ self . v = & self . v [ 1 ..] ;
2227
2225
}
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)
2226
+ ret
2237
2227
}
2238
2228
2239
2229
#[ inline]
2240
2230
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2241
- ( self . num , Some ( self . num ) )
2231
+ let size = self . v . len ( ) . saturating_sub ( N - 1 ) ;
2232
+ ( size, Some ( size) )
2242
2233
}
2243
2234
2244
2235
#[ inline]
2245
2236
fn count ( self ) -> usize {
2246
- self . num
2237
+ self . len ( )
2247
2238
}
2248
2239
2249
2240
#[ inline]
2250
2241
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)
2242
+ let idx = n. min ( self . v . len ( ) ) ;
2243
+ self . v = & self . v [ idx..] ;
2244
+ self . next ( )
2263
2245
}
2264
2246
2265
2247
#[ inline]
2266
- fn last ( mut self ) -> Option < Self :: Item > {
2267
- self . nth ( self . num . checked_sub ( 1 ) ? )
2248
+ fn last ( self ) -> Option < Self :: Item > {
2249
+ self . v . last_chunk ( )
2268
2250
}
2269
2251
}
2270
2252
2271
2253
#[ unstable( feature = "array_windows" , issue = "75027" ) ]
2272
2254
impl < ' a , T , const N : usize > DoubleEndedIterator for ArrayWindows < ' a , T , N > {
2273
2255
#[ inline]
2274
2256
fn next_back ( & mut self ) -> Option < & ' a [ T ; N ] > {
2275
- if self . num == 0 {
2276
- return None ;
2257
+ let ret = self . v . last_chunk ( ) ;
2258
+ if ret. is_some ( ) {
2259
+ self . v = & self . v [ ..self . v . len ( ) - 1 ] ;
2277
2260
}
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)
2261
+ ret
2282
2262
}
2283
2263
2284
2264
#[ inline]
2285
2265
fn nth_back ( & mut self , n : usize ) -> Option < & ' a [ T ; N ] > {
2286
- if self . num <= n {
2287
- self . num = 0 ;
2288
- return None ;
2289
- }
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)
2266
+ let idx = self . v . len ( ) . saturating_sub ( n) ;
2267
+ self . v = & self . v [ ..idx] ;
2268
+ self . next_back ( )
2294
2269
}
2295
2270
}
2296
2271
2297
2272
#[ unstable( feature = "array_windows" , issue = "75027" ) ]
2298
2273
impl < T , const N : usize > ExactSizeIterator for ArrayWindows < ' _ , T , N > {
2299
2274
fn is_empty ( & self ) -> bool {
2300
- self . num == 0
2275
+ self . v . len ( ) < N
2301
2276
}
2302
2277
}
2303
2278
0 commit comments