aboutsummaryrefslogtreecommitdiff
path: root/src/terminal
diff options
context:
space:
mode:
authorEric Yue <jiacheng.yue@foxmail.com>2026-09-28 03:34:03 +0800
committerGitHub <noreply@github.com>2026-09-27 21:34:03 +0200
commit7945e91e30b91a379fcbbd68cf2100fe122205cd (patch)
tree1da54071d681bc22da34576bb251e90701e75bfb /src/terminal
parent0b9c8ead0073221aaf4f1b0a5e01f0c44a61ad0e (diff)
perf: use keyed terminal lookup for named targets (#4669)
Diffstat (limited to 'src/terminal')
-rw-r--r--src/terminal/id.rs27
1 files changed, 27 insertions, 0 deletions
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<str> 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());
+ }
+}