-
Notifications
You must be signed in to change notification settings - Fork 3
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
Support RangeBounds
for T: Comparable
#4
base: main
Are you sure you want to change the base?
Conversation
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 think having a separate trait will make this awkward to use, because that's an additional constraint on the range type that you'd have to propagate in API. How about something like this added to Comparable
(with a default impl):
fn compare_contains<R: RangeBounds<Self>>(range: R, key: &K) -> bool;
It will be more awkward to use without the self
receiver though, so I'm not sure this is better -- just an alternative to consider.
This would also keep the "direction" consistent, like my other comment.
src/lib.rs
Outdated
/// ``` | ||
fn comparable_contains<U>(&self, item: &U) -> bool | ||
where | ||
U: ?Sized + Comparable<T>, |
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.
What is the intended use case? If it's something like BTreeMap::range
, then I think the relationship should be the other way, T: Comparable<U>
, or in map terms it's RangeBounds<Q>
and Q: Comparable<K>
.
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 intended use case is most like a BTreeMap
, I need something like
fn foo<R, Q>(&self, key: &K)
where
R: RangeBounds<Q>,
Q: ?Sized + Comparable<K>
{
if self.range.comparable_contains(key) { }
}
Yeah, another trait will require users to import it, but if there is no |
Hmm, my thought was more that you would have to add the trait as a constraint in your public API, but I guess that's not true -- with the blanket impl, you can still use it as long as
Yes, or I'd write |
I pushed a few updates myself. I'm still hesitating whether this really makes sense as However, your example looks like your user is providing a |
If you have a real-world example in public code, that would really help me see what you're doing. I know it probably seems really nitpicky, but this is a 1.0 crate in the public API of other crates, so bumping semver is costly -- I don't want to add something we'll regret. (like #5) If we can figure out decent names for either direction, we could perhaps have it both ways, like: pub trait ComparableRangeBounds<T: ?Sized>: RangeBounds<T> {
fn compare_contains_k<K>(&self, item: &K) -> bool
where
K: ?Sized,
T: Comparable<K>;
fn compare_contains_q<Q>(&self, item: &Q) -> bool
where
Q: ?Sized + Comparable<T>;
} |
Hi, although, |
Hi, this PR adds functionality like
RangeBounds::contains
forT: Comparable
.