]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #75377 - canova:map_debug_impl, r=dtolnay
authorJonas Schievink <jonasschievink@gmail.com>
Fri, 2 Oct 2020 22:31:04 +0000 (00:31 +0200)
committerGitHub <noreply@github.com>
Fri, 2 Oct 2020 22:31:04 +0000 (00:31 +0200)
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

1  2 
library/alloc/src/collections/btree/map.rs
library/std/src/collections/hash/map.rs

index 3fb03a5412e4f9276e1cc84cecb9e9060ef7aa81,a08c19e3df4e9ff63760c8f49728e6a75da4580c..2b244a04d22231cb49bb143be1d165b025fd8227
@@@ -1946,9 -2017,18 +1981,18 @@@ impl<'a, K, V> RangeMut<'a, K, V> 
          self.front == self.back
      }
  
 -    unsafe fn next_unchecked(&mut self) -> (&'a mut K, &'a mut V) {
 +    unsafe fn next_unchecked(&mut self) -> (&'a 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")]