From 5b5a56d9ea7682b478f6f264693e7cb3afc06cc1 Mon Sep 17 00:00:00 2001 From: Ticki Date: Mon, 28 Dec 2015 23:34:08 +0100 Subject: [PATCH 1/2] 'Contains' method for ranges --- text/0000-contains-method-for-ranges.md | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 text/0000-contains-method-for-ranges.md diff --git a/text/0000-contains-method-for-ranges.md b/text/0000-contains-method-for-ranges.md new file mode 100644 index 00000000000..a072fac9866 --- /dev/null +++ b/text/0000-contains-method-for-ranges.md @@ -0,0 +1,77 @@ +- Feature Name: contains_method +- Start Date: 2015-12-28 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary +[summary]: #summary + +Implement a method, `contains()`, for `Range`, `RangeFrom`, and `RangeTo`, checking if a number is in the range. + +Note that the alternatives are just as important as the main proposal. + +# Motivation +[motivation]: #motivation + +The motivation behind this is simple: To be able to write simpler and more expressive code. This RFC introduces a "syntactic sugar" without doing so. + +# Detailed design +[design]: #detailed-design + +Implement a method, `contains()`, for `Range`, `RangeFrom`, and `RangeTo`. This method will check if a number is bound by the range. It will yield a boolean based on the condition defined by the range. + +The implementation is as follows (placed in libcore, and reexported by libstd): + +```rust +use core::ops::{Range, RangeTo, RangeFrom}; + +impl Range where Idx: PartialOrd { + fn contains(&self, item: Idx) -> bool { + self.start <= item && self.end > item + } +} + +impl RangeTo where Idx: PartialOrd { + fn contains(&self, item: Idx) -> bool { + self.end > item + } +} + +impl RangeFrom where Idx: PartialOrd { + fn contains(&self, item: Idx) -> bool { + self.start <= item + } +} + +``` + +# Drawbacks +[drawbacks]: #drawbacks + +Lacks of generics (see Alternatives). + +# Alternatives +[alternatives]: #alternatives + +## Add a `Contains` trait + +This trait provides the method `.contains()` and implements it for all the Range types. + +## Add a `.contains(item: Self::Item)` iterator method + +This method returns a boolean, telling if the iterator contains the item given as parameter. Using method specialization, this can achieve the same performance as the method suggested in this RFC. + +This is more flexible, and provide better performance (due to specialization) than just passing a closure comparing the items to a `any()` method. + +## Make `.any()` generic over a new trait + +Call this trait, `ItemPattern`. This trait is implemented for `Item` and `FnMut(Item) -> bool`. This is, in a sense, similar to `std::str::pattern::Pattern`. + +Then let `.any()` generic over this trait (`T: ItemPattern`) to allow `any()` taking `Self::Item` searching through the iterator for this particular value. + +This will not achieve the same performance as the other proposals. + +# Unresolved questions +[unresolved]: #unresolved-questions + +None. From a57d6c46c1f28d0a4467718b9c00c5ae5ade4400 Mon Sep 17 00:00:00 2001 From: Ticki Date: Mon, 28 Dec 2015 23:54:31 +0100 Subject: [PATCH 2/2] Fix mistake in the trait bound --- text/0000-contains-method-for-ranges.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-contains-method-for-ranges.md b/text/0000-contains-method-for-ranges.md index a072fac9866..0ee3d1cc91f 100644 --- a/text/0000-contains-method-for-ranges.md +++ b/text/0000-contains-method-for-ranges.md @@ -57,7 +57,7 @@ Lacks of generics (see Alternatives). This trait provides the method `.contains()` and implements it for all the Range types. -## Add a `.contains(item: Self::Item)` iterator method +## Add a `.contains>(i: I)` iterator method This method returns a boolean, telling if the iterator contains the item given as parameter. Using method specialization, this can achieve the same performance as the method suggested in this RFC.