From 7945e91e30b91a379fcbbd68cf2100fe122205cd Mon Sep 17 00:00:00 2001 From: Eric Yue Date: Mon, 28 Sep 2026 03:34:03 +0800 Subject: perf: use keyed terminal lookup for named targets (#4669) --- src/terminal/id.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/terminal/id.rs') diff --git a/src/terminal/id.rs b/src/terminal/id.rs index c7310753..29a300a5 100644 --- a/src/terminal/id.rs +++ b/src/terminal/id.rs @@ -1,3 +1,4 @@ +use std::borrow::Borrow; use std::fmt; use std::sync::atomic::{AtomicU64, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; @@ -31,3 +32,29 @@ impl fmt::Display for TerminalId { f.write_str(&self.0) } } + +impl Borrow for TerminalId { + fn borrow(&self) -> &str { + self.as_str() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + #[test] + fn terminal_ids_support_borrowed_lookup_without_changing_identity() { + let id = TerminalId::alloc(); + let serialized = serde_json::to_string(&id).unwrap(); + let restored: TerminalId = serde_json::from_str(&serialized).unwrap(); + let mut terminals = HashMap::from([(id.clone(), 1)]); + + assert_eq!(terminals.get(restored.as_str()), Some(&1)); + assert_eq!(terminals.get("missing-terminal"), None); + *terminals.get_mut(restored.as_str()).unwrap() = 2; + assert_eq!(terminals.remove(id.as_str()), Some(2)); + assert!(terminals.is_empty()); + } +} -- cgit v1.2.3