Skip to content

Commit

Permalink
Correct the implementation of Debug for ValuesMut and IntoValues stru…
Browse files Browse the repository at this point in the history
…ctures

Previously the implementation of Debug trait for ValuesMut was
```
impl<K, V> fmt::Debug for ValuesMut<'_, K, V>
where
    K: fmt::Debug,
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.inner.iter()).finish()
    }
}
```
It is strange, because `self.inner.iter()` return an iterator with element of type `(&’a K, &’a V)`.

The same is true when we look at the old implementation of Debug trait for the `IntoValues` structure. Here we have mistake
```
impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.inner.iter().map(|(k, _)| k))
            .finish()
    }
}
```
because with the function `self.inner.iter().map(|(k, _)| k)` we return iterator with element of type 'a K.
  • Loading branch information
JustForFun88 committed Apr 27, 2022
1 parent 21bfd9a commit fb7e3b1
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,10 +1942,10 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {

impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}

impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
impl<K, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.inner.iter().map(|(k, _)| k))
.entries(self.inner.iter().map(|(_, v)| v))
.finish()
}
}
Expand Down Expand Up @@ -3149,13 +3149,9 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
}
impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}

impl<K, V> fmt::Debug for ValuesMut<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
impl<K, V: Debug> fmt::Debug for ValuesMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.inner.iter()).finish()
f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
}
}

Expand Down

0 comments on commit fb7e3b1

Please sign in to comment.