Skip to content

Commit eb77056

Browse files
authored
Rollup merge of rust-lang#58924 - cuviper:more-as_slice, r=dtolnay
Add as_slice() to slice::IterMut and vec::Drain In indexmap-rs/indexmap#88, we found that there was no easy way to implement `Debug` for our `IterMut` and `Drain` iterators. Those are built on `slice::IterMut` and `vec::Drain`, which implement `Debug` themselves, but have no other way to access their data. With a new `as_slice()` method, we can read the data and customize its presentation.
2 parents 5753b9e + e478cad commit eb77056

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/liballoc/vec.rs

+19
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,25 @@ impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
24682468
}
24692469
}
24702470

2471+
impl<'a, T> Drain<'a, T> {
2472+
/// Returns the remaining items of this iterator as a slice.
2473+
///
2474+
/// # Examples
2475+
///
2476+
/// ```
2477+
/// # #![feature(vec_drain_as_slice)]
2478+
/// let mut vec = vec!['a', 'b', 'c'];
2479+
/// let mut drain = vec.drain(..);
2480+
/// assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
2481+
/// let _ = drain.next().unwrap();
2482+
/// assert_eq!(drain.as_slice(), &['b', 'c']);
2483+
/// ```
2484+
#[unstable(feature = "vec_drain_as_slice", reason = "recently added", issue = "58957")]
2485+
pub fn as_slice(&self) -> &[T] {
2486+
self.iter.as_slice()
2487+
}
2488+
}
2489+
24712490
#[stable(feature = "drain", since = "1.6.0")]
24722491
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
24732492
#[stable(feature = "drain", since = "1.6.0")]

src/libcore/slice/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,34 @@ impl<'a, T> IterMut<'a, T> {
32883288
pub fn into_slice(self) -> &'a mut [T] {
32893289
unsafe { from_raw_parts_mut(self.ptr, len!(self)) }
32903290
}
3291+
3292+
/// Views the underlying data as a subslice of the original data.
3293+
///
3294+
/// To avoid creating `&mut [T]` references that alias, the returned slice
3295+
/// borrows its lifetime from the iterator the method is applied on.
3296+
///
3297+
/// # Examples
3298+
///
3299+
/// Basic usage:
3300+
///
3301+
/// ```
3302+
/// # #![feature(slice_iter_mut_as_slice)]
3303+
/// let mut slice: &mut [usize] = &mut [1, 2, 3];
3304+
///
3305+
/// // First, we get the iterator:
3306+
/// let mut iter = slice.iter_mut();
3307+
/// // So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
3308+
/// assert_eq!(iter.as_slice(), &[1, 2, 3]);
3309+
///
3310+
/// // Next, we move to the second element of the slice:
3311+
/// iter.next();
3312+
/// // Now `as_slice` returns "[2, 3]":
3313+
/// assert_eq!(iter.as_slice(), &[2, 3]);
3314+
/// ```
3315+
#[unstable(feature = "slice_iter_mut_as_slice", reason = "recently added", issue = "58957")]
3316+
pub fn as_slice(&self) -> &[T] {
3317+
self.make_slice()
3318+
}
32913319
}
32923320

32933321
iterator!{struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}

0 commit comments

Comments
 (0)