-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Fix: Relax Interner
trait bounds for borrowed elements
#14039
base: main
Are you sure you want to change the base?
Conversation
- Fix trait bounds in certain methods of `Interner` to allow use of elements that satisfy the trait bounds of `Equivalent<T>` in `IndexMap`.
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 13931421405Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
crates/circuit/src/interner.rs
Outdated
pub fn try_key<Q>(&self, value: &Q) -> Option<Interned<T>> | ||
where | ||
Q: ?Sized + Hash + Eq + ToOwned, | ||
T: Borrow<Q> + ToOwned<Owned = <Q as ToOwned>::Owned>, |
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.
Maybe we can instead reexport indexmap
's Equivalent
with pub use indexmap::Equivalent
in this file? And then just use it in these method bounds so it's easier to understand what's happening.
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.
I tried putting in Q: Equivalent<T>
alone, but then the compiler will complain because, to satisfy the trait bounds of Q: Equivalent<T>
, Q
needs to be: Q: ?Sized + ToOwned
, and T
needs to satisfy T: Borrow<Q>
as a minimum. The rest is more specificity to mention that the owned versions of both T
and Q
need to be of the same base type.
Bringing those trait bounds in already makes using Q: Equivalent<T>
redundant, but it is implicitly satisfied. Is there an easier wa of conveying this?
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.
Maybe this would work?
pub fn try_key<Q>(&self, value: &Q) -> Option<Interned<T>>
where
Q: ?Sized + Hash + Equivalent<T::Owned>,
{
todo!()
}
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.
This works as long as we also specify that T: Borrow<Q>
. Unfortunately, though, it's still not enough for the insert
method. Fixed in d5161e3!
Summary
Fix trait bounds in certain methods of
Interner
to allow for the use of elements that satisfy the trait bounds ofEquivalent<T>
inIndexMap
.Details and comments
In a very special edge case for the
Interner
a user was not able to check if it contained an instance ofString
from an&str
. The following commits relax the trait bounds on thecontains
,try_key
, andinsert
methods, to allow for more flexibility by behaving similarly to the traitEquivalent<T>
fromIndexMap
.