@@ -78,7 +78,7 @@ pub struct Iter<'a, T: 'a> {
78
78
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
79
79
impl < T : fmt:: Debug > fmt:: Debug for Iter < ' _ , T > {
80
80
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 ( )
82
82
}
83
83
}
84
84
@@ -129,11 +129,22 @@ impl<'a, T> Iter<'a, T> {
129
129
#[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
130
130
#[ inline]
131
131
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 ( )
133
144
}
134
145
}
135
146
136
- iterator ! { struct Iter -> * const T , & ' a T , const , { /* no mut */ } , as_ref , {
147
+ iterator ! { struct Iter -> * const T , & ' a T , {
137
148
fn is_sorted_by<F >( self , mut compare: F ) -> bool
138
149
where
139
150
Self : Sized ,
@@ -201,7 +212,7 @@ pub struct IterMut<'a, T: 'a> {
201
212
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
202
213
impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
203
214
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 ( )
205
216
}
206
217
}
207
218
@@ -307,7 +318,8 @@ impl<'a, T> IterMut<'a, T> {
307
318
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
308
319
#[ inline]
309
320
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 ( ) }
311
323
}
312
324
313
325
/// Views the underlying data as a mutable subslice of the original data.
@@ -350,6 +362,16 @@ impl<'a, T> IterMut<'a, T> {
350
362
// for `from_raw_parts_mut` are fulfilled.
351
363
unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
352
364
}
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
+ }
353
375
}
354
376
355
377
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -367,7 +389,7 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
367
389
// }
368
390
// }
369
391
370
- iterator ! { struct IterMut -> * mut T , & ' a mut T , mut , { mut } , as_mut , { } }
392
+ iterator ! { struct IterMut -> * mut T , & ' a mut T , { } }
371
393
372
394
/// An internal abstraction over the splitting iterators, so that
373
395
/// splitn, splitn_mut etc can be implemented once.
0 commit comments