Manually implement Debug for BTreeMap::ValuesMut struct

Deriving debug prints all the values including keys. But ValuesMut
struct should only print the values.
This commit is contained in:
Nazım Can Altınova 2020-08-10 17:49:39 +02:00
parent 456738e3d1
commit c346e89db8
No known key found for this signature in database
GPG Key ID: 722E786F0729647A

View File

@ -361,11 +361,17 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///
/// [`values_mut`]: BTreeMap::values_mut
#[stable(feature = "map_values_mut", since = "1.10.0")]
#[derive(Debug)]
pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
}
#[stable(feature = "map_values_mut", since = "1.10.0")]
impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
}
}
/// An owning iterator over the keys of a `BTreeMap`.
///
/// This `struct` is created by the [`into_keys`] method on [`BTreeMap`].
@ -1519,6 +1525,14 @@ fn len(&self) -> usize {
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
impl<'a, K, V> IterMut<'a, K, V> {
/// Returns an iterator of references over the remaining items.
#[inline]
pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter { range: self.range.iter(), length: self.length }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<K, V> IntoIterator for BTreeMap<K, V> {
type Item = (K, V);
@ -2006,6 +2020,15 @@ fn is_empty(&self) -> bool {
unsafe fn next_unchecked(&mut self) -> (&'a mut K, &'a mut V) {
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
}
/// Returns an iterator of references over the remaining items.
#[inline]
pub(super) fn iter(&self) -> Range<'_, K, V> {
Range {
front: self.front.as_ref().map(|f| f.reborrow()),
back: self.back.as_ref().map(|b| b.reborrow()),
}
}
}
#[stable(feature = "btree_range", since = "1.17.0")]