@@ -75,7 +75,7 @@ pub struct Iter<'a, T: 'a> {
7575#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
7676impl < T : fmt:: Debug > fmt:: Debug for Iter < ' _ , T > {
7777 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
78- f. debug_tuple ( "Iter" ) . field ( & self . as_slice ( ) ) . finish ( )
78+ f. debug_tuple ( "Iter" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
7979 }
8080}
8181
@@ -126,11 +126,22 @@ impl<'a, T> Iter<'a, T> {
126126 #[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
127127 #[ inline]
128128 pub fn as_slice ( & self ) -> & ' a [ T ] {
129- self . make_slice ( )
129+ // SAFETY: the type invariant guarantees the pointer represents a valid slice
130+ unsafe { self . make_nonnull_slice ( ) . as_ref ( ) }
131+ }
132+
133+ #[ inline]
134+ unsafe fn non_null_to_item ( p : NonNull < T > ) -> <Self as Iterator >:: Item {
135+ // SAFETY: the type invariant guarantees the pointer represents a valid reference
136+ unsafe { p. as_ref ( ) }
137+ }
138+
139+ fn empty ( ) -> Self {
140+ ( & [ ] ) . into_iter ( )
130141 }
131142}
132143
133- iterator ! { struct Iter -> * const T , & ' a T , const , { /* no mut */ } , as_ref , {
144+ iterator ! { struct Iter -> * const T , & ' a T , {
134145 fn is_sorted_by<F >( self , mut compare: F ) -> bool
135146 where
136147 Self : Sized ,
@@ -198,7 +209,7 @@ pub struct IterMut<'a, T: 'a> {
198209#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
199210impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
200211 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
201- f. debug_tuple ( "IterMut" ) . field ( & self . make_slice ( ) ) . finish ( )
212+ f. debug_tuple ( "IterMut" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
202213 }
203214}
204215
@@ -304,7 +315,8 @@ impl<'a, T> IterMut<'a, T> {
304315 #[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
305316 #[ inline]
306317 pub fn as_slice ( & self ) -> & [ T ] {
307- self . make_slice ( )
318+ // SAFETY: the type invariant guarantees the pointer represents a valid slice
319+ unsafe { self . make_nonnull_slice ( ) . as_ref ( ) }
308320 }
309321
310322 /// Views the underlying data as a mutable subslice of the original data.
@@ -347,6 +359,16 @@ impl<'a, T> IterMut<'a, T> {
347359 // for `from_raw_parts_mut` are fulfilled.
348360 unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
349361 }
362+
363+ #[ inline]
364+ unsafe fn non_null_to_item ( mut p : NonNull < T > ) -> <Self as Iterator >:: Item {
365+ // SAFETY: the type invariant guarantees the pointer represents a valid item
366+ unsafe { p. as_mut ( ) }
367+ }
368+
369+ fn empty ( ) -> Self {
370+ ( & mut [ ] ) . into_iter ( )
371+ }
350372}
351373
352374#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -364,7 +386,7 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
364386// }
365387// }
366388
367- iterator ! { struct IterMut -> * mut T , & ' a mut T , mut , { mut } , as_mut , { } }
389+ iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
368390
369391/// An internal abstraction over the splitting iterators, so that
370392/// splitn, splitn_mut etc can be implemented once.
0 commit comments