Skip to content

Commit

Permalink
improve IndexMap's Debug print (#1610)
Browse files Browse the repository at this point in the history
I was debugging an `IndexMap`, and I found the debug print to be
difficult to use. It didn't print out the keys for the map, and was
overly whitespace-heavy. I made this modification for my own debugging,
and thought it might be a good update to push upstream.

Before:

```
 IndexMap {
    values: [
        (
            1,
            Item(
                ItemId {
                    package: None,
                    item: LocalItemId(
                        0,
                    ),
                },
                Available,
                Imported,
            ),
        ),
        (
            5,
            Item(
                ItemId {
                    package: None,
                    item: LocalItemId(
                        1,
                    ),
                },
                Available,
                Debug(
                    10,
                ),
            ),
        ),
        (
            8,
            PrimTy(
                Int,
            ),
        ),
        (
            15,
            Item(
                ItemId {
                    package: None,
                    item: LocalItemId(
                        1,
                    ),
                },
                Available,
                Debug(
                    10,
                ),
            ),
        ),
... etc
```



After:

```
IndexMap {
    values: [
        "1: Item(ItemId { package: None, item: LocalItemId(0) }, Available, Imported)",
        "5: Item(ItemId { package: None, item: LocalItemId(1) }, Available, Debug(10))",
        "8: PrimTy(Int)",
        "15: Item(ItemId { package: None, item: LocalItemId(1) }, Available, Debug(10))",
        "16: Item(ItemId { package: None, item: LocalItemId(1) }, Available, Debug(10))",
    ],
}
```


No `expect-test` stuff relies on this print, so it is a pretty isolated
change.
  • Loading branch information
sezna authored Jun 6, 2024
1 parent c085de5 commit 5a1054e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion compiler/qsc_data_structures/src/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ impl<K, V: Clone> Clone for IndexMap<K, V> {
impl<K, V: Debug> Debug for IndexMap<K, V> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("IndexMap")
.field("values", &self.values)
.field(
"values",
&self
.values
.iter()
.enumerate()
.filter_map(|(k, v)| v.as_ref().map(|val| format!("{k:?}: {val:?}")))
.collect::<Vec<_>>(),
)
.finish()
}
}
Expand Down

0 comments on commit 5a1054e

Please sign in to comment.