@@ -306,6 +306,47 @@ impl<'a, T> IterMut<'a, T> {
306
306
pub fn as_slice ( & self ) -> & [ T ] {
307
307
self . make_slice ( )
308
308
}
309
+
310
+ /// Views the underlying data as a mutable subslice of the original data.
311
+ ///
312
+ /// To avoid creating `&mut [T]` references that alias, the returned slice
313
+ /// borrows its lifetime from the iterator the method is applied on.
314
+ ///
315
+ /// # Examples
316
+ ///
317
+ /// Basic usage:
318
+ ///
319
+ /// ```
320
+ /// #![feature(slice_iter_mut_as_mut_slice)]
321
+ ///
322
+ /// let mut slice: &mut [usize] = &mut [1, 2, 3];
323
+ ///
324
+ /// // First, we get the iterator:
325
+ /// let mut iter = slice.iter_mut();
326
+ /// // Then, we get a mutable slice from it:
327
+ /// let mut_slice = iter.as_mut_slice();
328
+ /// // So if we check what the `as_mut_slice` method returned, we have "[1, 2, 3]":
329
+ /// assert_eq!(mut_slice, &mut [1, 2, 3]);
330
+ ///
331
+ /// // We can use it to mutate the slice:
332
+ /// mut_slice[0] = 4;
333
+ /// mut_slice[2] = 5;
334
+ ///
335
+ /// // Next, we can move to the second element of the slice, checking that
336
+ /// // it yields the value we just wrote:
337
+ /// assert_eq!(iter.next(), Some(&mut 4));
338
+ /// // Now `as_mut_slice` returns "[2, 5]":
339
+ /// assert_eq!(iter.as_mut_slice(), &mut [2, 5]);
340
+ /// ```
341
+ #[ must_use]
342
+ // FIXME: Uncomment the `AsMut<[T]>` impl when this gets stabilized.
343
+ #[ unstable( feature = "slice_iter_mut_as_mut_slice" , issue = "93079" ) ]
344
+ pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
345
+ // SAFETY: the iterator was created from a mutable slice with pointer
346
+ // `self.ptr` and length `len!(self)`. This guarantees that all the prerequisites
347
+ // for `from_raw_parts_mut` are fulfilled.
348
+ unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
349
+ }
309
350
}
310
351
311
352
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -315,6 +356,13 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
315
356
}
316
357
}
317
358
359
+ // #[stable(feature = "slice_iter_mut_as_mut_slice", since = "FIXME")]
360
+ // impl<T> AsMut<[T]> for IterMut<'_, T> {
361
+ // fn as_mut(&mut self) -> &mut [T] {
362
+ // self.as_mut_slice()
363
+ // }
364
+ // }
365
+
318
366
iterator ! { struct IterMut -> * mut T , & ' a mut T , mut , { mut } , { } }
319
367
320
368
/// An internal abstraction over the splitting iterators, so that
0 commit comments