Skip to content

Commit fd9ca0c

Browse files
authored
Rollup merge of rust-lang#93080 - SkiFire13:itermut-as_mut_slice, r=m-ou-se
Implement `core::slice::IterMut::as_mut_slice` and `impl<T> AsMut<[T]> for IterMut<'_, T>` As per [the zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/.60std.3A.3Aslice.3A.3AIterMut.3A.3Aas_mut_slice.60), the `AsMut` impl has been commented out, with a comment near the `#[unstable(...)]` to uncomment it when `as_mut_slice` gets stabilized.
2 parents 4104596 + c867529 commit fd9ca0c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

library/core/src/slice/iter.rs

+48
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,47 @@ impl<'a, T> IterMut<'a, T> {
306306
pub fn as_slice(&self) -> &[T] {
307307
self.make_slice()
308308
}
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+
}
309350
}
310351

311352
#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
@@ -315,6 +356,13 @@ impl<T> AsRef<[T]> for IterMut<'_, T> {
315356
}
316357
}
317358

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+
318366
iterator! {struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}
319367

320368
/// An internal abstraction over the splitting iterators, so that

0 commit comments

Comments
 (0)