-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for Range*::contains #32311
Comments
I am unsure what part of the rfc was accepted. Is the |
The accepted portion of an RFC is the detailed design, which in this case only deals with ranges, not iterators. |
Well, okay. Fine. |
It is in fact also compatible with future extension to iterators. |
Should this be implemented for (unstable) |
For consistency, yeah, seems prudent. |
Signed-off-by: NODA, Kai <nodakai@gmail.com>
Add core::ops::Range*::contains() as per #32311
Hm, I just used this and expected it to take Looking back the RFC, it does not seem to consider this case - was there any discussion for this possibility? |
Hi, This RFC doesn't address
|
Hm, my elation turned into sadness today when I added a
I can only imagine that method resolution works this way for the sake of backwards compatibility, but... yikes! Since this change is "compatible with future extension to iterators," I take it that there is a way to implement an |
@rfcbot fcp merge Seem like nifty methods to stabilize! |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot concern previous-questions I see three previous comments asking about the API here. I don't know anything about this API but it's not obvious it's ready for stabilization. |
@rfcbot fcp cancel Ok, sounds like these aren't ready yet. |
@alexcrichton proposal cancelled. |
A thought on impl<Idx, T> Range<Idx> where T:PartialOrd<Idx>, Idx:PartialOrd<T> {
pub fn contains(&self, item: T) -> bool {
(self.start <= item) && (item < self.end)
}
} So it's totally fine if Idx=T (the two constraints collapse to the current one, and a caller could just require Idx:PartialOrd), but would allow checking a (Edit: adding backticks so the angle brackets show up) |
Any status update on this? |
Ping? |
The FCP for merge was canceled due to unresolved questions, and it looks like those unresolved questions have not been resolved. |
Just regarding the questions about the current implementation:
I'd suggest the most reasonable implementation would be something like: impl<Idx> Range<Idx> {
pub fn contains<T>(&self, item: &T) -> bool
where Idx: PartialOrd, Idx: PartialOrd<T> {
if self.start <= self.end {
self.start <= *item && self.end > *item
} else {
self.start <= *item || self.end > *item
}
}
} 1 is going to break existing uses; 2 should be implementable without breaking anything; 3 changes behaviour, so would be a breaking change. As the current implementation falls short of the expected behaviour, it seems to me like this is a change worth making. |
Though I still feel it's better to handle the |
It would be nice if you could make it a trait instead of a directly implemented method so that you can see if a range contains a number without having to worry about whether it's a RangeFull, Range, RangeFrom, etc. It would enable you to do something like fn pi_in_range<T: ContainRange>(range: T) -> bool {
range.contains(3.141592653589793238462643383)
}
// ...
assert!(pi_in_range(0.0 .. 10.0);
assert!(pi_in_range(..10.0);
assert!(!pi_in_range(10.0..);
assert!(pi_in_range(..); |
I'd like to second @lukaramu's and others' comments above, I really think this should be moved to be a trait method on RangeArgument instead of implemented on each range type exclusively. There could probably even be a default impl that just depends on start and end and works for everything. Edit: See my PR below which does this. |
@lukaramu why you don't believe |
Move Range*::contains to a single default impl on RangeBounds Per the ongoing discussion in #32311. This is my first PR to Rust (woo!), so I don't know if this requires an amendment to the original range_contains RFC, or not, or if we can just do a psuedo-RFC here. While this may no longer follow the explicit decision made in that RFC, I believe this better follows its spirit by adding the new contains method to all Ranges. It also allows users to be generic over all ranges and use this method without writing it themselves (my personal desired use case). This also somewhat answers the unanswered question about Wrapping ranges in the above issue by instead just punting it to the question of what those types should return for start() & end(), or if they should implement RangeArgument at all. Those types could also implement their own contains method without implementing this trait, in which case the question remains the same. This does add a new contains method to types that already implemented RangeArgument but not contains. These types are RangeFull, (Bound<T>, Bound<T>), (Bound<&'a T>, Bound<&'a T>). No tests have been added for these types yet. No inherent method has been added either. r? @alexcrichton
The two Implementation Work items have now been done. The question about Wrapping types still remains. |
My personal opinion on the question of Wrapping types: If you look at the new contains implementation it's hard to argue that it could be incorrect in some way. It seems completely reasonable to say that a range contains something iff that something is greater than the start and less than the end. If the Wrapping types don't work within these guidelines that feels to me more like a bug in their PartialOrd implementation. This could potentially be solved with specialization though. |
I'd like to suggest this for stabilization. I agree with the previous comment that the current implementation is fine for (If we had never implemented Ord and PartialOrd for Wrapping, then we could argue differently, but it is too late for that.) |
cc @SimonSapin |
The comments above on @rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Stabilize Range*::contains. Closes rust-lang#32311. There's also a bit of rustfmt on range.rs thrown in for good measure (I forgot to turn off format-on-save in VSCode).
Stabilize Range*::contains. Closes rust-lang#32311. There's also a bit of rustfmt on range.rs thrown in for good measure (I forgot to turn off format-on-save in VSCode).
Stabilize Range*::contains. Closes rust-lang#32311. There's also a bit of rustfmt on range.rs thrown in for good measure (I forgot to turn off format-on-save in VSCode).
Stabilize Range*::contains. Closes rust-lang#32311. There's also a bit of rustfmt on range.rs thrown in for good measure (I forgot to turn off format-on-save in VSCode).
Tracking issue for rust-lang/rfcs#1434.
Unresolved questions:
Wrapping
ranges.Implementation work:
The text was updated successfully, but these errors were encountered: