From 718a1ee34068542ab31c98023896dc21cf7c30ce Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 11 Mar 2022 14:00:21 -0500 Subject: [PATCH] Add a more generic implementation of intern-by-ref --- crates/tor-netdoc/src/doc/netstatus/rs.rs | 2 +- crates/tor-netdoc/src/util/intern.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/tor-netdoc/src/doc/netstatus/rs.rs b/crates/tor-netdoc/src/doc/netstatus/rs.rs index 0f9dd22de..457198d7e 100644 --- a/crates/tor-netdoc/src/doc/netstatus/rs.rs +++ b/crates/tor-netdoc/src/doc/netstatus/rs.rs @@ -81,7 +81,7 @@ impl std::str::FromStr for Version { } } - Ok(Version::Other(OTHER_VERSION_CACHE.intern(s))) + Ok(Version::Other(OTHER_VERSION_CACHE.intern_ref(s))) } } diff --git a/crates/tor-netdoc/src/util/intern.rs b/crates/tor-netdoc/src/util/intern.rs index fcfacc3ee..3e662def3 100644 --- a/crates/tor-netdoc/src/util/intern.rs +++ b/crates/tor-netdoc/src/util/intern.rs @@ -55,9 +55,17 @@ impl InternCache { } } -impl InternCache { - /// Intern a string slice into this cache. - pub(crate) fn intern(&self, value: &str) -> Arc { +impl InternCache { + /// Intern an object by reference. + /// + /// Works with unsized types, but requires that the reference implements + /// `Into>`. + pub(crate) fn intern_ref<'a, V>(&self, value: &'a V) -> Arc + where + V: Hash + Eq + ?Sized, + &'a V: Into>, + T: std::borrow::Borrow, + { let mut cache = self.cache(); if let Some(arc) = cache.get(value) { arc