Skip to content

Commit

Permalink
tweak is_interned to use only Borrow type constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Sep 14, 2024
1 parent 1e07522 commit d650464
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/boxedset.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::ops::Deref;
use hashbrown::HashMap;
use std::{
borrow::Borrow,
hash::{BuildHasher, Hash, Hasher},
borrow::Borrow, hash::{BuildHasher, Hash, Hasher}
};
pub struct HashSet<P>(HashMap<P, ()>);

Expand Down
25 changes: 25 additions & 0 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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::<String>::from_ref("Fortunato");
assert!(Intern::<String>::is_interned("Fortunato"));
assert!(!Intern::<String>::is_interned("Montresor"));
}

#[cfg(feature = "deepsize")]
Expand Down Expand Up @@ -245,6 +248,28 @@ impl<T: Eq + Hash + Send + Sync + 'static + ?Sized> Intern<T> {
pub fn benchmarking_only_clear_interns() {
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| m.clear())
}
/// Check if a value already is interned.
///
/// If this value has previously been interned, return true, else returns false/// Checking if an object is already interned
///
/// ```rust
///
/// use internment::Intern;
///
/// assert!(!Intern::<String>::is_interned("Fortunato"));
/// let x = Intern::new("Fortunato".to_string());
/// assert!(Intern::<String>::is_interned("Fortunato"));
///
/// assert!(!Intern::<str>::is_interned("Fortunato"));
/// let x: Intern<str> = "Fortunato".into();
/// assert!(Intern::<str>::is_interned("Fortunato"));
/// ```
pub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> bool
where
T: Borrow<Q>,
{
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| -> bool { m.get(val).is_some() })
}
}

#[cfg(feature = "bench")]
Expand Down

0 comments on commit d650464

Please sign in to comment.