Skip to content

Commit 1118ab9

Browse files
Rollup merge of #75377 - canova:map_debug_impl, r=dtolnay
Fix Debug implementations of some of the HashMap and BTreeMap iterator types HashMap's `ValuesMut`, BTreeMaps `ValuesMut`, IntoValues and `IntoKeys` structs were printing both keys and values on their Debug implementations. But they are iterators over either keys or values. Irrelevant values should not be visible. With this PR, they only show relevant fields. This fixes #75297. [Here's an example code.](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0c79356ed860e347a0c1a205616f93b7) This prints this on nightly: ``` ValuesMut { inner: IterMut { range: [(1, "hello"), (2, "goodbye")], length: 2 } } IntoKeys { inner: [(1, "hello"), (2, "goodbye")] } IntoValues { inner: [(1, "hello"), (2, "goodbye")] } [(2, "goodbye"), (1, "hello")] ``` After the patch this example prints these instead: ``` ["hello", "goodbye"] ["hello", "goodbye"] [1, 2] ["hello", "goodbye"] ``` I didn't add test cases for them, since I couldn't see any tests for Debug implementations anywhere. But please let me know if I should add it to a specific place. r? @dtolnay
2 parents 8876ffc + 8faf550 commit 1118ab9

File tree

2 files changed

+55
-15
lines changed
  • library
    • alloc/src/collections/btree
    • std/src/collections/hash

2 files changed

+55
-15
lines changed

library/alloc/src/collections/btree/map.rs

+51-7
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,23 @@ pub struct IntoIter<K, V> {
297297
length: usize,
298298
}
299299

300-
#[stable(feature = "collection_debug", since = "1.17.0")]
301-
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
302-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
300+
impl<K, V> IntoIter<K, V> {
301+
/// Returns an iterator of references over the remaining items.
302+
#[inline]
303+
pub(super) fn iter(&self) -> Iter<'_, K, V> {
303304
let range = Range {
304305
front: self.front.as_ref().map(|f| f.reborrow()),
305306
back: self.back.as_ref().map(|b| b.reborrow()),
306307
};
307-
f.debug_list().entries(range).finish()
308+
309+
Iter { range: range, length: self.length }
310+
}
311+
}
312+
313+
#[stable(feature = "collection_debug", since = "1.17.0")]
314+
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
315+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
316+
f.debug_list().entries(self.iter()).finish()
308317
}
309318
}
310319

@@ -351,35 +360,53 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
351360
///
352361
/// [`values_mut`]: BTreeMap::values_mut
353362
#[stable(feature = "map_values_mut", since = "1.10.0")]
354-
#[derive(Debug)]
355363
pub struct ValuesMut<'a, K: 'a, V: 'a> {
356364
inner: IterMut<'a, K, V>,
357365
}
358366

367+
#[stable(feature = "map_values_mut", since = "1.10.0")]
368+
impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
369+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
370+
f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
371+
}
372+
}
373+
359374
/// An owning iterator over the keys of a `BTreeMap`.
360375
///
361376
/// This `struct` is created by the [`into_keys`] method on [`BTreeMap`].
362377
/// See its documentation for more.
363378
///
364379
/// [`into_keys`]: BTreeMap::into_keys
365380
#[unstable(feature = "map_into_keys_values", issue = "75294")]
366-
#[derive(Debug)]
367381
pub struct IntoKeys<K, V> {
368382
inner: IntoIter<K, V>,
369383
}
370384

385+
#[unstable(feature = "map_into_keys_values", issue = "75294")]
386+
impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
387+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
388+
f.debug_list().entries(self.inner.iter().map(|(key, _)| key)).finish()
389+
}
390+
}
391+
371392
/// An owning iterator over the values of a `BTreeMap`.
372393
///
373394
/// This `struct` is created by the [`into_values`] method on [`BTreeMap`].
374395
/// See its documentation for more.
375396
///
376397
/// [`into_values`]: BTreeMap::into_values
377398
#[unstable(feature = "map_into_keys_values", issue = "75294")]
378-
#[derive(Debug)]
379399
pub struct IntoValues<K, V> {
380400
inner: IntoIter<K, V>,
381401
}
382402

403+
#[unstable(feature = "map_into_keys_values", issue = "75294")]
404+
impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
405+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
406+
f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
407+
}
408+
}
409+
383410
/// An iterator over a sub-range of entries in a `BTreeMap`.
384411
///
385412
/// This `struct` is created by the [`range`] method on [`BTreeMap`]. See its
@@ -1465,6 +1492,14 @@ impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
14651492
#[stable(feature = "fused", since = "1.26.0")]
14661493
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
14671494

1495+
impl<'a, K, V> IterMut<'a, K, V> {
1496+
/// Returns an iterator of references over the remaining items.
1497+
#[inline]
1498+
pub(super) fn iter(&self) -> Iter<'_, K, V> {
1499+
Iter { range: self.range.iter(), length: self.length }
1500+
}
1501+
}
1502+
14681503
#[stable(feature = "rust1", since = "1.0.0")]
14691504
impl<K, V> IntoIterator for BTreeMap<K, V> {
14701505
type Item = (K, V);
@@ -1949,6 +1984,15 @@ impl<'a, K, V> RangeMut<'a, K, V> {
19491984
unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
19501985
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
19511986
}
1987+
1988+
/// Returns an iterator of references over the remaining items.
1989+
#[inline]
1990+
pub(super) fn iter(&self) -> Range<'_, K, V> {
1991+
Range {
1992+
front: self.front.as_ref().map(|f| f.reborrow()),
1993+
back: self.back.as_ref().map(|b| b.reborrow()),
1994+
}
1995+
}
19521996
}
19531997

19541998
#[stable(feature = "btree_range", since = "1.17.0")]

library/std/src/collections/hash/map.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2042,13 +2042,9 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
20422042
impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
20432043

20442044
#[stable(feature = "std_debug", since = "1.16.0")]
2045-
impl<K, V> fmt::Debug for ValuesMut<'_, K, V>
2046-
where
2047-
K: fmt::Debug,
2048-
V: fmt::Debug,
2049-
{
2045+
impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
20502046
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2051-
f.debug_list().entries(self.inner.iter()).finish()
2047+
f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
20522048
}
20532049
}
20542050

@@ -2076,7 +2072,7 @@ impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
20762072
impl<K, V> FusedIterator for IntoKeys<K, V> {}
20772073

20782074
#[unstable(feature = "map_into_keys_values", issue = "75294")]
2079-
impl<K: Debug, V: Debug> fmt::Debug for IntoKeys<K, V> {
2075+
impl<K: Debug, V> fmt::Debug for IntoKeys<K, V> {
20802076
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20812077
f.debug_list().entries(self.inner.iter().map(|(k, _)| k)).finish()
20822078
}
@@ -2106,7 +2102,7 @@ impl<K, V> ExactSizeIterator for IntoValues<K, V> {
21062102
impl<K, V> FusedIterator for IntoValues<K, V> {}
21072103

21082104
#[unstable(feature = "map_into_keys_values", issue = "75294")]
2109-
impl<K: Debug, V: Debug> fmt::Debug for IntoValues<K, V> {
2105+
impl<K, V: Debug> fmt::Debug for IntoValues<K, V> {
21102106
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21112107
f.debug_list().entries(self.inner.iter().map(|(_, v)| v)).finish()
21122108
}

0 commit comments

Comments
 (0)