Skip to content

Commit 18b42ef

Browse files
authored
Improve debug formatting for primary/secondary map (#11081)
* Improve debug formatting for primary/secondary map * clippy suggestion * review suggestions
1 parent e813a92 commit 18b42ef

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

cranelift/entity/src/map.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::iter::{Iter, IterMut};
55
use crate::keys::Keys;
66
use alloc::vec::Vec;
77
use core::cmp::min;
8+
use core::fmt;
89
use core::marker::PhantomData;
910
use core::ops::{Index, IndexMut};
1011
use core::slice;
@@ -23,7 +24,7 @@ use serde::{
2324
///
2425
/// The map does not track if an entry for a key has been inserted or not. Instead it behaves as if
2526
/// all keys have a default entry from the beginning.
26-
#[derive(Debug, Clone, Hash)]
27+
#[derive(Clone, Hash)]
2728
pub struct SecondaryMap<K, V>
2829
where
2930
K: EntityRef,
@@ -282,6 +283,15 @@ where
282283
}
283284
}
284285

286+
impl<K: EntityRef + fmt::Debug, V: fmt::Debug + Clone> fmt::Debug for SecondaryMap<K, V> {
287+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288+
f.debug_struct("SecondaryMap")
289+
.field("elems", &self.elems)
290+
.field("default", &self.default)
291+
.finish()
292+
}
293+
}
294+
285295
#[cfg(test)]
286296
mod tests {
287297
use super::*;

cranelift/entity/src/primary.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::iter::{IntoIter, Iter, IterMut};
55
use crate::keys::Keys;
66
use alloc::boxed::Box;
77
use alloc::vec::Vec;
8+
use core::fmt;
89
use core::marker::PhantomData;
910
use core::mem;
1011
use core::ops::{Index, IndexMut};
@@ -27,7 +28,7 @@ use serde_derive::{Deserialize, Serialize};
2728
/// that it only allows indexing with the distinct `EntityRef` key type, so converting to a
2829
/// plain slice would make it easier to use incorrectly. To make a slice of a `PrimaryMap`, use
2930
/// `into_boxed_slice`.
30-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
31+
#[derive(Clone, Hash, PartialEq, Eq)]
3132
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
3233
pub struct PrimaryMap<K, V>
3334
where
@@ -336,6 +337,16 @@ where
336337
}
337338
}
338339

340+
impl<K: EntityRef + fmt::Debug, V: fmt::Debug> fmt::Debug for PrimaryMap<K, V> {
341+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
342+
let mut struct_ = f.debug_struct("PrimaryMap");
343+
for (k, v) in self {
344+
struct_.field(&alloc::format!("{k:?}"), v);
345+
}
346+
struct_.finish()
347+
}
348+
}
349+
339350
#[cfg(test)]
340351
mod tests {
341352
use super::*;

0 commit comments

Comments
 (0)