-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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<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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: You can remove the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, one sec. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I move the is_interned to the
Both check as valid, but when I run the test, |
||||||||||||||
{ | ||||||||||||||
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 | ||||||||||||||
|
There was a problem hiding this comment.
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
tois_interned
?There was a problem hiding this comment.
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("") checksIntern<str>
which are different.