@@ -78,7 +78,7 @@ pub struct Iter<'a, T: 'a> {
7878#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
7979impl < T : fmt:: Debug > fmt:: Debug for Iter < ' _ , T > {
8080 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
81- f. debug_tuple ( "Iter" ) . field ( & self . as_slice ( ) ) . finish ( )
81+ f. debug_tuple ( "Iter" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
8282 }
8383}
8484
@@ -129,11 +129,22 @@ impl<'a, T> Iter<'a, T> {
129129 #[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
130130 #[ inline]
131131 pub fn as_slice ( & self ) -> & ' a [ T ] {
132- self . make_slice ( )
132+ // SAFETY: the type invariant guarantees the pointer represents a valid slice
133+ unsafe { self . make_nonnull_slice ( ) . as_ref ( ) }
134+ }
135+
136+ #[ inline]
137+ unsafe fn non_null_to_item ( p : NonNull < T > ) -> <Self as Iterator >:: Item {
138+ // SAFETY: the type invariant guarantees the pointer represents a valid reference
139+ unsafe { p. as_ref ( ) }
140+ }
141+
142+ fn empty ( ) -> Self {
143+ ( & [ ] ) . into_iter ( )
133144 }
134145}
135146
136- iterator ! { struct Iter -> * const T , & ' a T , const , { /* no mut */ } , as_ref , {
147+ iterator ! { struct Iter -> * const T , & ' a T , {
137148 fn is_sorted_by<F >( self , mut compare: F ) -> bool
138149 where
139150 Self : Sized ,
@@ -201,7 +212,7 @@ pub struct IterMut<'a, T: 'a> {
201212#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
202213impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
203214 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
204- f. debug_tuple ( "IterMut" ) . field ( & self . make_slice ( ) ) . finish ( )
215+ f. debug_tuple ( "IterMut" ) . field ( & self . make_shortlived_slice ( ) ) . finish ( )
205216 }
206217}
207218
@@ -307,7 +318,8 @@ impl<'a, T> IterMut<'a, T> {
307318 #[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
308319 #[ inline]
309320 pub fn as_slice ( & self ) -> & [ T ] {
310- self . make_slice ( )
321+ // SAFETY: the type invariant guarantees the pointer represents a valid slice
322+ unsafe { self . make_nonnull_slice ( ) . as_ref ( ) }
311323 }
312324
313325 /// Views the underlying data as a mutable subslice of the original data.
@@ -350,6 +362,16 @@ impl<'a, T> IterMut<'a, T> {
350362 // for `from_raw_parts_mut` are fulfilled.
351363 unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
352364 }
365+
366+ #[ inline]
367+ unsafe fn non_null_to_item ( mut p : NonNull < T > ) -> <Self as Iterator >:: Item {
368+ // SAFETY: the type invariant guarantees the pointer represents a valid item
369+ unsafe { p. as_mut ( ) }
370+ }
371+
372+ fn empty ( ) -> Self {
373+ ( & mut [ ] ) . into_iter ( )
374+ }
353375}
354376
355377#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -367,7 +389,7 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
367389// }
368390// }
369391
370- iterator ! { struct IterMut -> * mut T , & ' a mut T , mut , { mut } , as_mut , { } }
392+ iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
371393
372394/// An internal abstraction over the splitting iterators, so that
373395/// splitn, splitn_mut etc can be implemented once.
0 commit comments