diff options
| author | Josh Stone <jistone@redhat.com> | 2019-03-04 15:12:45 -0800 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2019-03-04 15:12:45 -0800 |
| commit | 538a0963ff4a55da47fcf2a1e14c62edab5af48d (patch) | |
| tree | 4591fdc1ab918bebaef703d2b2b80237ab07c97a /src/liballoc/vec.rs | |
| parent | a9da8fc9c267c08cfdb8cf5b39da14f154d12939 (diff) | |
| download | rust-538a0963ff4a55da47fcf2a1e14c62edab5af48d.tar.gz rust-538a0963ff4a55da47fcf2a1e14c62edab5af48d.zip | |
Add as_slice() to slice::IterMut and vec::Drain
In bluss/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.
Diffstat (limited to 'src/liballoc/vec.rs')
| -rw-r--r-- | src/liballoc/vec.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 947ce354ae7..7c3cab77bfb 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2468,6 +2468,25 @@ impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> { } } +impl<'a, T> Drain<'a, T> { + /// Returns the remaining items of this iterator as a slice. + /// + /// # Examples + /// + /// ``` + /// # #![feature(vec_drain_as_slice)] + /// let mut vec = vec!['a', 'b', 'c']; + /// let mut drain = vec.drain(..); + /// assert_eq!(drain.as_slice(), &['a', 'b', 'c']); + /// let _ = drain.next().unwrap(); + /// assert_eq!(drain.as_slice(), &['b', 'c']); + /// ``` + #[unstable(feature = "vec_drain_as_slice", reason = "recently added", issue = "0")] + pub fn as_slice(&self) -> &[T] { + self.iter.as_slice() + } +} + #[stable(feature = "drain", since = "1.6.0")] unsafe impl<T: Sync> Sync for Drain<'_, T> {} #[stable(feature = "drain", since = "1.6.0")] |
