Skip to content

Commit 538a096

Browse files
committed
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.
1 parent a9da8fc commit 538a096

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-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 = "0")]
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

+32
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,38 @@ 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` references that alias, this has a
3295+
/// borrowed lifetime from the iterator.
3296+
///
3297+
/// # Examples
3298+
///
3299+
/// Basic usage:
3300+
///
3301+
/// ```
3302+
/// # #![feature(slice_iter_mut_as_slice)]
3303+
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
3304+
/// // struct (&[usize here]):
3305+
/// let mut slice = &mut [1, 2, 3];
3306+
///
3307+
/// // Then, we get the iterator:
3308+
/// let mut iter = slice.iter_mut();
3309+
/// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]":
3310+
/// println!("{:?}", iter.as_slice());
3311+
/// assert_eq!(iter.as_slice(), &[1, 2, 3]);
3312+
///
3313+
/// // Next, we move to the second element of the slice:
3314+
/// iter.next();
3315+
/// // Now `as_slice` returns "[2, 3]":
3316+
/// println!("{:?}", iter.as_slice());
3317+
/// assert_eq!(iter.as_slice(), &[2, 3]);
3318+
/// ```
3319+
#[unstable(feature = "slice_iter_mut_as_slice", reason = "recently added", issue = "0")]
3320+
pub fn as_slice(&self) -> &[T] {
3321+
self.make_slice()
3322+
}
32913323
}
32923324

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

0 commit comments

Comments
 (0)