Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added is_interned method which returns a bool #57

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/boxedset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ impl<P: deepsize::DeepSizeOf + ?Sized> deepsize::DeepSizeOf for HashSet<Box<P>>
}

impl<P: Deref + Eq + Hash> HashSet<P> {
pub fn contains<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
where
P: Borrow<Q>,
{
self.0.contains_key(k)
}
pub fn get<'a, Q: ?Sized + Eq + Hash>(&'a self, key: &Q) -> Option<&'a P>
where
P::Target: Borrow<Q>,
Expand Down
22 changes: 22 additions & 0 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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"));
Comment on lines +65 to +66
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Could you also test if we can pass just a &str to is_interned?

Suggested change
assert!(Intern::is_interned(&"Fortunato"));
assert!(!Intern::is_interned(&"Montresor"));
assert!(Intern::is_interned(&"Fortunato"));
assert!(!Intern::is_interned("Fortunato"));
assert!(!Intern::is_interned(&"Montresor"));
assert!(!Intern::is_interned("Montresor"));

Copy link
Author

@DanielJoyce DanielJoyce Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the && and & cases can't be unified trivially. Or I don't understand enough rust black magic to do so.

Basically Intern::is_interned(&"") checks Intern<&str> and Intern::is_interned("") checks Intern<str> which are different.

}

#[cfg_attr(docsrs, doc(cfg(feature = "intern")))]
Expand Down Expand Up @@ -224,6 +237,15 @@ impl<T: Eq + Hash + Send + Sync + 'static> Intern<T> {
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<Q> + From<&'a Q>,
Comment on lines +243 to +245
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: You can remove the From constraint for this method.

Suggested change
pub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> bool
where
&'static T: Borrow<Q> + From<&'a Q>,
pub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> bool
where
&'static T: Borrow<Q>,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do, then I need to specify generic bounds on line 72:

type annotations needed
cannot satisfy `_: std::cmp::Eq`rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B24%5D?24#file%3A%2F%2F%2Fhome%2Fdanieljoyce%2Fgit%2Fgeneral%2Finternment%2Fsrc%2Fintern.rs)
intern.rs(204, 9): required by a bound in `Intern::<T>::is_interned`
intern.rs(72, 19): consider specifying the generic argument: `::<T>`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, one sec.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the From<> bounds on T, then it requires dismbiguation per the above error message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I move the is_interned to the impl<T: Eq + Hash + Send + Sync + 'static + ?Sized> Intern<T> { block, then

 let _ = Intern::new("Fortunato");
    assert!(Intern::is_interned(&"Fortunato"));
    assert!(Intern::is_interned("Fortunato"));

Both check as valid, but when I run the test, assert!(Intern::is_interned("Fortunato")); fails.

{
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| -> bool { m.contains(val) })
}
}
impl<T: Eq + Hash + Send + Sync + 'static + ?Sized> Intern<T> {
/// Get a long-lived reference to the data pointed to by an `Intern`, which
Expand Down
Loading