@@ -135,13 +135,23 @@ impl<'a, T> Iter<'a, T> {
135135 // SAFETY: the type invariant guarantees the pointer represents a valid reference
136136 unsafe { p. as_ref ( ) }
137137 }
138+ }
138139
139- fn empty ( ) -> Self {
140+ #[ stable( feature = "default_iters" , since = "1.70.0" ) ]
141+ impl < T > Default for Iter < ' _ , T > {
142+ /// Creates an empty slice iterator.
143+ ///
144+ /// ```
145+ /// # use core::slice::Iter;
146+ /// let iter: Iter<'_, u8> = Default::default();
147+ /// assert_eq!(iter.len(), 0);
148+ /// ```
149+ fn default ( ) -> Self {
140150 ( & [ ] ) . into_iter ( )
141151 }
142152}
143153
144- iterator ! { struct Iter - > * const T , & ' a T , {
154+ iterator ! { struct Iter < ' a , T > = > * const T , & ' a T , {
145155 fn is_sorted_by<F >( self , mut compare: F ) -> bool
146156 where
147157 Self : Sized ,
@@ -365,8 +375,18 @@ impl<'a, T> IterMut<'a, T> {
365375 // SAFETY: the type invariant guarantees the pointer represents a valid item
366376 unsafe { p. as_mut ( ) }
367377 }
378+ }
368379
369- fn empty ( ) -> Self {
380+ #[ stable( feature = "default_iters" , since = "1.70.0" ) ]
381+ impl < T > Default for IterMut < ' _ , T > {
382+ /// Creates an empty slice iterator.
383+ ///
384+ /// ```
385+ /// # use core::slice::IterMut;
386+ /// let iter: IterMut<'_, u8> = Default::default();
387+ /// assert_eq!(iter.len(), 0);
388+ /// ```
389+ fn default ( ) -> Self {
370390 ( & mut [ ] ) . into_iter ( )
371391 }
372392}
@@ -386,7 +406,85 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
386406// }
387407// }
388408
389- iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
409+ iterator ! { struct IterMut <' a, T > => * mut T , & ' a mut T , { } }
410+
411+ /// Iterator over all the `NonNull<T>` pointers to the elements of a slice.
412+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
413+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
414+ pub struct NonNullIter < T > {
415+ /// The pointer to the next element to return, or the past-the-end location
416+ /// if the iterator is empty.
417+ ///
418+ /// This address will be used for all ZST elements, never changed.
419+ ptr : NonNull < T > ,
420+ /// For non-ZSTs, the non-null pointer to the past-the-end element.
421+ ///
422+ /// For ZSTs, this is `ptr::without_provenance(len)`.
423+ end_or_len : * const T ,
424+ }
425+
426+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
427+ impl < T : fmt:: Debug > fmt:: Debug for NonNullIter < T > {
428+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
429+ f. debug_tuple ( "NonNullIter" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
430+ }
431+ }
432+
433+ impl < T > NonNullIter < T > {
434+ /// Turn an iterator giving `&T`s into one giving `NonNull<T>`s.
435+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
436+ pub fn from_slice_iter ( Iter { ptr, end_or_len, .. } : Iter < ' _ , T > ) -> Self {
437+ Self { ptr, end_or_len }
438+ }
439+
440+ /// Turn an iterator giving `&mut T`s into one giving `NonNull<T>`s.
441+ #[ unstable( feature = "slice_non_null_iter" , issue = "none" ) ]
442+ pub fn from_slice_iter_mut ( IterMut { ptr, end_or_len, .. } : IterMut < ' _ , T > ) -> Self {
443+ Self { ptr, end_or_len }
444+ }
445+
446+ /// Creates a new iterator over the `len` items starting at `ptr`
447+ ///
448+ /// # Safety
449+ ///
450+ /// - `ptr` through `ptr.add(len)` must be a single allocated object
451+ /// such that that it's sound to `offset` through it.
452+ /// - All those elements must be readable
453+ /// - The caller must ensure both as long as the iterator is in use.
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 >, { } }
390488
391489/// An internal abstraction over the splitting iterators, so that
392490/// splitn, splitn_mut etc can be implemented once.
0 commit comments