@@ -75,7 +75,7 @@ pub struct Iter<'a, T: 'a> {
75
75
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
76
76
impl < T : fmt:: Debug > fmt:: Debug for Iter < ' _ , T > {
77
77
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 ( )
79
79
}
80
80
}
81
81
@@ -126,11 +126,22 @@ impl<'a, T> Iter<'a, T> {
126
126
#[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
127
127
#[ inline]
128
128
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 ( )
130
141
}
131
142
}
132
143
133
- iterator ! { struct Iter -> * const T , & ' a T , const , { /* no mut */ } , as_ref , {
144
+ iterator ! { struct Iter -> * const T , & ' a T , {
134
145
fn is_sorted_by<F >( self , mut compare: F ) -> bool
135
146
where
136
147
Self : Sized ,
@@ -198,7 +209,7 @@ pub struct IterMut<'a, T: 'a> {
198
209
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
199
210
impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
200
211
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 ( )
202
213
}
203
214
}
204
215
@@ -304,7 +315,8 @@ impl<'a, T> IterMut<'a, T> {
304
315
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
305
316
#[ inline]
306
317
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 ( ) }
308
320
}
309
321
310
322
/// Views the underlying data as a mutable subslice of the original data.
@@ -347,6 +359,16 @@ impl<'a, T> IterMut<'a, T> {
347
359
// for `from_raw_parts_mut` are fulfilled.
348
360
unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
349
361
}
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
+ }
350
372
}
351
373
352
374
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -364,7 +386,7 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
364
386
// }
365
387
// }
366
388
367
- iterator ! { struct IterMut -> * mut T , & ' a mut T , mut , { mut } , as_mut , { } }
389
+ iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
368
390
369
391
/// An internal abstraction over the splitting iterators, so that
370
392
/// splitn, splitn_mut etc can be implemented once.
0 commit comments