@@ -135,13 +135,24 @@ impl<'a, T> Iter<'a, T> {
135
135
// SAFETY: the type invariant guarantees the pointer represents a valid reference
136
136
unsafe { p. as_ref ( ) }
137
137
}
138
+ }
138
139
139
- fn empty ( ) -> Self {
140
+ #[ allow( unused_lifetimes) ]
141
+ #[ stable( feature = "default_iters" , since = "1.70.0" ) ]
142
+ impl < T > Default for Iter < ' _ , T > {
143
+ /// Creates an empty slice iterator.
144
+ ///
145
+ /// ```
146
+ /// # use core::slice::Iter;
147
+ /// let iter: Iter<'_, u8> = Default::default();
148
+ /// assert_eq!(iter.len(), 0);
149
+ /// ```
150
+ fn default ( ) -> Self {
140
151
( & [ ] ) . into_iter ( )
141
152
}
142
153
}
143
154
144
- iterator ! { struct Iter - > * const T , & ' a T , {
155
+ iterator ! { struct Iter < ' a , T > = > * const T , & ' a T , {
145
156
fn is_sorted_by<F >( self , mut compare: F ) -> bool
146
157
where
147
158
Self : Sized ,
@@ -365,8 +376,19 @@ impl<'a, T> IterMut<'a, T> {
365
376
// SAFETY: the type invariant guarantees the pointer represents a valid item
366
377
unsafe { p. as_mut ( ) }
367
378
}
379
+ }
368
380
369
- fn empty ( ) -> Self {
381
+ #[ allow( unused_lifetimes) ]
382
+ #[ stable( feature = "default_iters" , since = "1.70.0" ) ]
383
+ impl < T > Default for IterMut < ' _ , T > {
384
+ /// Creates an empty slice iterator.
385
+ ///
386
+ /// ```
387
+ /// # use core::slice::IterMut;
388
+ /// let iter: IterMut<'_, u8> = Default::default();
389
+ /// assert_eq!(iter.len(), 0);
390
+ /// ```
391
+ fn default ( ) -> Self {
370
392
( & mut [ ] ) . into_iter ( )
371
393
}
372
394
}
@@ -386,7 +408,83 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
386
408
// }
387
409
// }
388
410
389
- iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
411
+ iterator ! { struct IterMut <' a, T > => * mut T , & ' a mut T , { } }
412
+
413
+ /// Iterator over all the `NonNull<T>` pointers to the elements of a slice.
414
+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
415
+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
416
+ pub struct NonNullIter < T > {
417
+ /// The pointer to the next element to return, or the past-the-end location
418
+ /// if the iterator is empty.
419
+ ///
420
+ /// This address will be used for all ZST elements, never changed.
421
+ ptr : NonNull < T > ,
422
+ /// For non-ZSTs, the non-null pointer to the past-the-end element.
423
+ ///
424
+ /// For ZSTs, this is `ptr::without_provenance_mut(len)`.
425
+ end_or_len : * const T ,
426
+ }
427
+
428
+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
429
+ impl < T : fmt:: Debug > fmt:: Debug for NonNullIter < T > {
430
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
431
+ f. debug_tuple ( "NonNullIter" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
432
+ }
433
+ }
434
+
435
+ impl < T > NonNullIter < T > {
436
+ /// Turn an iterator giving `&T`s into one giving `NonNull<T>`s.
437
+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
438
+ pub fn from_slice_iter ( Iter { ptr, end_or_len, .. } : Iter < ' _ , T > ) -> Self {
439
+ Self { ptr, end_or_len }
440
+ }
441
+
442
+ /// Turn an iterator giving `&mut T`s into one giving `NonNull<T>`s.
443
+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
444
+ pub fn from_slice_iter_mut ( IterMut { ptr, end_or_len, .. } : IterMut < ' _ , T > ) -> Self {
445
+ Self { ptr, end_or_len }
446
+ }
447
+
448
+ /// Creates a new iterator over the `len` items starting at `ptr`
449
+ ///
450
+ /// # Safety
451
+ ///
452
+ /// `ptr` through `ptr.add(len)` must be a single allocated object
453
+ /// such that that
454
+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
455
+ #[ inline]
456
+ pub unsafe fn from_parts ( ptr : NonNull < T > , len : usize ) -> Self {
457
+ // SAFETY: There are several things here:
458
+ //
459
+ // `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
460
+ // reference thus it is non-NUL and safe to use and pass to
461
+ // `NonNull::new_unchecked` .
462
+ //
463
+ // Adding `slice.len()` to the starting pointer gives a pointer
464
+ // at the end of `slice`. `end` will never be dereferenced, only checked
465
+ // for direct pointer equality with `ptr` to check if the iterator is
466
+ // done.
467
+ //
468
+ // In the case of a ZST, the end pointer is just the length. It's never
469
+ // used as a pointer at all, and thus it's fine to have no provenance.
470
+ //
471
+ // See the `next_unchecked!` and `is_empty!` macros as well as the
472
+ // `post_inc_start` method for more information.
473
+ unsafe {
474
+ let end_or_len =
475
+ if T :: IS_ZST { without_provenance_mut ( len) } else { ptr. as_ptr ( ) . add ( len) } ;
476
+
477
+ Self { ptr, end_or_len }
478
+ }
479
+ }
480
+
481
+ #[ inline]
482
+ unsafe fn non_null_to_item ( p : NonNull < T > ) -> <Self as Iterator >:: Item {
483
+ p
484
+ }
485
+ }
486
+
487
+ iterator ! { struct NonNullIter <T > => * const T , NonNull <T >, { } }
390
488
391
489
/// An internal abstraction over the splitting iterators, so that
392
490
/// splitn, splitn_mut etc can be implemented once.
0 commit comments