From 5662bd8524275e9ba03579074796032e4790b077 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 29 Jul 2024 15:55:12 -0500 Subject: [PATCH] cl-structures: (ab)use the Display trait to print a numbered, sorted list of interned strings. --- compiler/cl-structures/src/intern.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/compiler/cl-structures/src/intern.rs b/compiler/cl-structures/src/intern.rs index e52e6d1..4e3e7f9 100644 --- a/compiler/cl-structures/src/intern.rs +++ b/compiler/cl-structures/src/intern.rs @@ -185,6 +185,24 @@ pub mod string_interner { } } + impl std::fmt::Display for StringInterner<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Ok(keys) = self.keys.read() else { + return write!(f, "Could not lock StringInterner key map."); + }; + let mut keys: Vec<_> = keys.iter().collect(); + keys.sort(); + + writeln!(f, "Keys:")?; + for (idx, key) in keys.iter().enumerate() { + writeln!(f, "{idx}:\t\"{key}\"")? + } + writeln!(f, "Count: {}", keys.len())?; + + Ok(()) + } + } + // # Safety: // This is fine because StringInterner::get_or_insert(v) holds a RwLock // for its entire duration, and doesn't touch the non-(Send+Sync) arena