Skip to content

Commit 86fc63e

Browse files
committed
Implement fmt::Debug for all structures in libstd.
Part of #31869. Also turn on the `missing_debug_implementations` lint at the crate level.
1 parent 1f965cc commit 86fc63e

31 files changed

+515
-7
lines changed

src/libstd/ascii.rs

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

15+
use fmt;
1516
use mem;
1617
use ops::Range;
1718
use iter::FusedIterator;
@@ -370,6 +371,13 @@ impl ExactSizeIterator for EscapeDefault {}
370371
#[unstable(feature = "fused", issue = "35602")]
371372
impl FusedIterator for EscapeDefault {}
372373

374+
#[stable(feature = "std_debug", since = "1.15.0")]
375+
impl fmt::Debug for EscapeDefault {
376+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
377+
f.pad("EscapeDefault { .. }")
378+
}
379+
}
380+
373381

374382
static ASCII_LOWERCASE_MAP: [u8; 256] = [
375383
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

src/libstd/collections/hash/map.rs

+81-2
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,15 @@ impl<'a, K, V> Clone for Iter<'a, K, V> {
12761276
}
12771277
}
12781278

1279+
#[stable(feature = "std_debug", since = "1.15.0")]
1280+
impl<'a, K: Debug, V: Debug> fmt::Debug for Iter<'a, K, V> {
1281+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1282+
f.debug_list()
1283+
.entries(self.clone())
1284+
.finish()
1285+
}
1286+
}
1287+
12791288
/// HashMap mutable values iterator.
12801289
#[stable(feature = "rust1", since = "1.0.0")]
12811290
pub struct IterMut<'a, K: 'a, V: 'a> {
@@ -1285,7 +1294,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
12851294
/// HashMap move iterator.
12861295
#[stable(feature = "rust1", since = "1.0.0")]
12871296
pub struct IntoIter<K, V> {
1288-
inner: table::IntoIter<K, V>,
1297+
pub(super) inner: table::IntoIter<K, V>,
12891298
}
12901299

12911300
/// HashMap keys iterator.
@@ -1302,6 +1311,15 @@ impl<'a, K, V> Clone for Keys<'a, K, V> {
13021311
}
13031312
}
13041313

1314+
#[stable(feature = "std_debug", since = "1.15.0")]
1315+
impl<'a, K: Debug, V: Debug> fmt::Debug for Keys<'a, K, V> {
1316+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1317+
f.debug_list()
1318+
.entries(self.clone())
1319+
.finish()
1320+
}
1321+
}
1322+
13051323
/// HashMap values iterator.
13061324
#[stable(feature = "rust1", since = "1.0.0")]
13071325
pub struct Values<'a, K: 'a, V: 'a> {
@@ -1316,10 +1334,19 @@ impl<'a, K, V> Clone for Values<'a, K, V> {
13161334
}
13171335
}
13181336

1337+
#[stable(feature = "std_debug", since = "1.15.0")]
1338+
impl<'a, K: Debug, V: Debug> fmt::Debug for Values<'a, K, V> {
1339+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1340+
f.debug_list()
1341+
.entries(self.clone())
1342+
.finish()
1343+
}
1344+
}
1345+
13191346
/// HashMap drain iterator.
13201347
#[stable(feature = "drain", since = "1.6.0")]
13211348
pub struct Drain<'a, K: 'a, V: 'a> {
1322-
inner: table::Drain<'a, K, V>,
1349+
pub(super) inner: table::Drain<'a, K, V>,
13231350
}
13241351

13251352
/// Mutable HashMap values iterator.
@@ -1557,6 +1584,18 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
15571584
#[unstable(feature = "fused", issue = "35602")]
15581585
impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
15591586

1587+
#[stable(feature = "std_debug", since = "1.15.0")]
1588+
impl<'a, K, V> fmt::Debug for IterMut<'a, K, V>
1589+
where K: fmt::Debug,
1590+
V: fmt::Debug,
1591+
{
1592+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1593+
f.debug_list()
1594+
.entries(self.inner.iter())
1595+
.finish()
1596+
}
1597+
}
1598+
15601599
#[stable(feature = "rust1", since = "1.0.0")]
15611600
impl<K, V> Iterator for IntoIter<K, V> {
15621601
type Item = (K, V);
@@ -1580,6 +1619,15 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
15801619
#[unstable(feature = "fused", issue = "35602")]
15811620
impl<K, V> FusedIterator for IntoIter<K, V> {}
15821621

1622+
#[stable(feature = "std_debug", since = "1.15.0")]
1623+
impl<K: Debug, V: Debug> fmt::Debug for IntoIter<K, V> {
1624+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1625+
f.debug_list()
1626+
.entries(self.inner.iter())
1627+
.finish()
1628+
}
1629+
}
1630+
15831631
#[stable(feature = "rust1", since = "1.0.0")]
15841632
impl<'a, K, V> Iterator for Keys<'a, K, V> {
15851633
type Item = &'a K;
@@ -1649,6 +1697,18 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
16491697
#[unstable(feature = "fused", issue = "35602")]
16501698
impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {}
16511699

1700+
#[stable(feature = "std_debug", since = "1.15.0")]
1701+
impl<'a, K, V> fmt::Debug for ValuesMut<'a, K, V>
1702+
where K: fmt::Debug,
1703+
V: fmt::Debug,
1704+
{
1705+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1706+
f.debug_list()
1707+
.entries(self.inner.inner.iter())
1708+
.finish()
1709+
}
1710+
}
1711+
16521712
#[stable(feature = "drain", since = "1.6.0")]
16531713
impl<'a, K, V> Iterator for Drain<'a, K, V> {
16541714
type Item = (K, V);
@@ -1672,6 +1732,18 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
16721732
#[unstable(feature = "fused", issue = "35602")]
16731733
impl<'a, K, V> FusedIterator for Drain<'a, K, V> {}
16741734

1735+
#[stable(feature = "std_debug", since = "1.15.0")]
1736+
impl<'a, K, V> fmt::Debug for Drain<'a, K, V>
1737+
where K: fmt::Debug,
1738+
V: fmt::Debug,
1739+
{
1740+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1741+
f.debug_list()
1742+
.entries(self.inner.iter())
1743+
.finish()
1744+
}
1745+
}
1746+
16751747
impl<'a, K, V> Entry<'a, K, V> {
16761748
#[stable(feature = "rust1", since = "1.0.0")]
16771749
/// Ensures a value is in the entry by inserting the default if empty, and returns
@@ -2148,6 +2220,13 @@ impl Default for RandomState {
21482220
}
21492221
}
21502222

2223+
#[stable(feature = "std_debug", since = "1.15.0")]
2224+
impl fmt::Debug for RandomState {
2225+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2226+
f.pad("RandomState { .. }")
2227+
}
2228+
}
2229+
21512230
impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
21522231
where K: Eq + Hash + Borrow<Q>,
21532232
S: BuildHasher,

src/libstd/collections/hash/set.rs

+77
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,15 @@ impl<'a, K> ExactSizeIterator for Iter<'a, K> {
948948
#[unstable(feature = "fused", issue = "35602")]
949949
impl<'a, K> FusedIterator for Iter<'a, K> {}
950950

951+
#[stable(feature = "std_debug", since = "1.15.0")]
952+
impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> {
953+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
954+
f.debug_list()
955+
.entries(self.clone())
956+
.finish()
957+
}
958+
}
959+
951960
#[stable(feature = "rust1", since = "1.0.0")]
952961
impl<K> Iterator for IntoIter<K> {
953962
type Item = K;
@@ -968,6 +977,16 @@ impl<K> ExactSizeIterator for IntoIter<K> {
968977
#[unstable(feature = "fused", issue = "35602")]
969978
impl<K> FusedIterator for IntoIter<K> {}
970979

980+
#[stable(feature = "std_debug", since = "1.15.0")]
981+
impl<K: fmt::Debug> fmt::Debug for IntoIter<K> {
982+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
983+
let entries_iter = self.iter.inner.iter().map(|(k, _)| k);
984+
f.debug_list()
985+
.entries(entries_iter)
986+
.finish()
987+
}
988+
}
989+
971990
#[stable(feature = "rust1", since = "1.0.0")]
972991
impl<'a, K> Iterator for Drain<'a, K> {
973992
type Item = K;
@@ -988,6 +1007,16 @@ impl<'a, K> ExactSizeIterator for Drain<'a, K> {
9881007
#[unstable(feature = "fused", issue = "35602")]
9891008
impl<'a, K> FusedIterator for Drain<'a, K> {}
9901009

1010+
#[stable(feature = "std_debug", since = "1.15.0")]
1011+
impl<'a, K: fmt::Debug> fmt::Debug for Drain<'a, K> {
1012+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1013+
let entries_iter = self.iter.inner.iter().map(|(k, _)| k);
1014+
f.debug_list()
1015+
.entries(entries_iter)
1016+
.finish()
1017+
}
1018+
}
1019+
9911020
#[stable(feature = "rust1", since = "1.0.0")]
9921021
impl<'a, T, S> Clone for Intersection<'a, T, S> {
9931022
fn clone(&self) -> Intersection<'a, T, S> {
@@ -1021,6 +1050,18 @@ impl<'a, T, S> Iterator for Intersection<'a, T, S>
10211050
}
10221051
}
10231052

1053+
#[stable(feature = "std_debug", since = "1.15.0")]
1054+
impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
1055+
where T: fmt::Debug + Eq + Hash,
1056+
S: BuildHasher,
1057+
{
1058+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1059+
f.debug_list()
1060+
.entries(self.clone())
1061+
.finish()
1062+
}
1063+
}
1064+
10241065
#[unstable(feature = "fused", issue = "35602")]
10251066
impl<'a, T, S> FusedIterator for Intersection<'a, T, S>
10261067
where T: Eq + Hash,
@@ -1068,6 +1109,18 @@ impl<'a, T, S> FusedIterator for Difference<'a, T, S>
10681109
{
10691110
}
10701111

1112+
#[stable(feature = "std_debug", since = "1.15.0")]
1113+
impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
1114+
where T: fmt::Debug + Eq + Hash,
1115+
S: BuildHasher,
1116+
{
1117+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1118+
f.debug_list()
1119+
.entries(self.clone())
1120+
.finish()
1121+
}
1122+
}
1123+
10711124
#[stable(feature = "rust1", since = "1.0.0")]
10721125
impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
10731126
fn clone(&self) -> SymmetricDifference<'a, T, S> {
@@ -1097,6 +1150,18 @@ impl<'a, T, S> FusedIterator for SymmetricDifference<'a, T, S>
10971150
{
10981151
}
10991152

1153+
#[stable(feature = "std_debug", since = "1.15.0")]
1154+
impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S>
1155+
where T: fmt::Debug + Eq + Hash,
1156+
S: BuildHasher,
1157+
{
1158+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1159+
f.debug_list()
1160+
.entries(self.clone())
1161+
.finish()
1162+
}
1163+
}
1164+
11001165
#[stable(feature = "rust1", since = "1.0.0")]
11011166
impl<'a, T, S> Clone for Union<'a, T, S> {
11021167
fn clone(&self) -> Union<'a, T, S> {
@@ -1111,6 +1176,18 @@ impl<'a, T, S> FusedIterator for Union<'a, T, S>
11111176
{
11121177
}
11131178

1179+
#[stable(feature = "std_debug", since = "1.15.0")]
1180+
impl<'a, T, S> fmt::Debug for Union<'a, T, S>
1181+
where T: fmt::Debug + Eq + Hash,
1182+
S: BuildHasher,
1183+
{
1184+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1185+
f.debug_list()
1186+
.entries(self.clone())
1187+
.finish()
1188+
}
1189+
}
1190+
11141191
#[stable(feature = "rust1", since = "1.0.0")]
11151192
impl<'a, T, S> Iterator for Union<'a, T, S>
11161193
where T: Eq + Hash,

src/libstd/collections/hash/table.rs

+29
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,15 @@ unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}
882882
// but Send is the more useful bound
883883
unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
884884

885+
impl<'a, K: 'a, V: 'a> IterMut<'a, K, V> {
886+
pub fn iter(&self) -> Iter<K, V> {
887+
Iter {
888+
iter: self.iter.clone(),
889+
elems_left: self.elems_left,
890+
}
891+
}
892+
}
893+
885894
/// Iterator over the entries in a table, consuming the table.
886895
pub struct IntoIter<K, V> {
887896
table: RawTable<K, V>,
@@ -891,6 +900,15 @@ pub struct IntoIter<K, V> {
891900
unsafe impl<K: Sync, V: Sync> Sync for IntoIter<K, V> {}
892901
unsafe impl<K: Send, V: Send> Send for IntoIter<K, V> {}
893902

903+
impl<K, V> IntoIter<K, V> {
904+
pub fn iter(&self) -> Iter<K, V> {
905+
Iter {
906+
iter: self.iter.clone(),
907+
elems_left: self.table.size,
908+
}
909+
}
910+
}
911+
894912
/// Iterator over the entries in a table, clearing the table.
895913
pub struct Drain<'a, K: 'a, V: 'a> {
896914
table: Shared<RawTable<K, V>>,
@@ -901,6 +919,17 @@ pub struct Drain<'a, K: 'a, V: 'a> {
901919
unsafe impl<'a, K: Sync, V: Sync> Sync for Drain<'a, K, V> {}
902920
unsafe impl<'a, K: Send, V: Send> Send for Drain<'a, K, V> {}
903921

922+
impl<'a, K, V> Drain<'a, K, V> {
923+
pub fn iter(&self) -> Iter<K, V> {
924+
unsafe {
925+
Iter {
926+
iter: self.iter.clone(),
927+
elems_left: (**self.table).size,
928+
}
929+
}
930+
}
931+
}
932+
904933
impl<'a, K, V> Iterator for Iter<'a, K, V> {
905934
type Item = (&'a K, &'a V);
906935

0 commit comments

Comments
 (0)