From aeceeca60ccf204abb391424f914dfd0882a86cd Mon Sep 17 00:00:00 2001 From: Daniel Joyce Date: Thu, 11 Jul 2024 07:57:08 -0700 Subject: [PATCH] Added is_interned method which returns a bool * Returns true if value already interned. * Added simple test as well. --- src/boxedset.rs | 6 ++++++ src/intern.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/boxedset.rs b/src/boxedset.rs index 15c98ec..4cf4837 100644 --- a/src/boxedset.rs +++ b/src/boxedset.rs @@ -44,6 +44,12 @@ impl deepsize::DeepSizeOf for HashSet> } impl HashSet

{ + pub fn contains(&mut self, k: &Q) -> bool + where + P: Borrow, + { + self.0.contains_key(k) + } pub fn get<'a, Q: ?Sized + Eq + Hash>(&'a self, key: &Q) -> Option<&'a P> where P::Target: Borrow, diff --git a/src/intern.rs b/src/intern.rs index 3ed38f9..fd99f3a 100644 --- a/src/intern.rs +++ b/src/intern.rs @@ -43,6 +43,16 @@ use tinyset::Fits64; /// assert_eq!(y, Intern::from("world")); /// assert_eq!(&*x, "hello"); // dereference a Intern like a pointer /// ``` +/// +/// Checking if an object is already interned +/// +/// ```rust +/// +/// use internment::Intern; +/// +/// let x = Intern::new("Fortunato"); +/// assert!(Intern::is_interned("Fortunato")); +/// ``` #[test] fn like_doctest_intern() { let x = Intern::new("hello".to_string()); @@ -51,6 +61,9 @@ fn like_doctest_intern() { assert_eq!(x, Intern::from_ref("hello")); assert_eq!(y, Intern::from_ref("world")); assert_eq!(&*x, "hello"); // dereference a Intern like a pointer\ + let _ = Intern::new("Fortunato"); + assert!(Intern::is_interned(&"Fortunato")); + assert!(!Intern::is_interned(&"Montresor")); } #[cfg_attr(docsrs, doc(cfg(feature = "intern")))] @@ -224,6 +237,15 @@ impl Intern { Intern { pointer: p } }) } + /// Check if a value already is interned. + /// + /// If this value has previously been interned, return true, else returns false + pub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> bool + where + &'static T: Borrow + From<&'a Q>, + { + INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| -> bool { m.contains(val) }) + } } impl Intern { /// Get a long-lived reference to the data pointed to by an `Intern`, which