From d118414806a8cb8c5c5d9a0f159fb8bf65c6b519 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sat, 9 Jan 2021 21:42:33 -0500 Subject: [PATCH 1/9] Add Itertools.contains --- src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0c6af37e4..8edfbe0fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1608,6 +1608,53 @@ pub trait Itertools : Iterator { None } + /// Check whether the iterator contains an item. + /// + /// If the iterator contains the item prior to its end, + /// this method will short-circuit and only partially + /// exhaust the iterator. + /// + /// ``` + /// use itertools::Itertools; + /// + /// let data = vec![ + /// "foo".to_owned(), + /// "bar".to_owned(), + /// "baz".to_owned(), + /// ]; + /// // this could get expensive: + /// assert!(data.contains(&"foo".to_owned())); + /// // this, less so: + /// assert!(data.iter().contains("foo")); + /// + /// // now the not-as-motivating tests involving Copy data: + /// + /// let data = vec![4usize, 5, 1, 3, 0, 2]; + /// assert!(data.iter().contains(&4)); + /// assert!(!data.iter().contains(&6)); + /// for x in (0..50) { + /// assert_eq!(data.contains(&x), data.iter().contains(&x)); + /// } + /// + /// let mut it = data.iter(); + /// assert!(!it.contains(&6)); + /// assert_eq!(it.next(), None); + /// + /// let mut it = data.iter(); + /// assert!(it.contains(&3)); + /// assert_eq!(it.next(), Some(&0)); + /// + /// let data : Option = None; + /// assert!(!data.into_iter().contains(0)); + /// ``` + fn contains(&mut self, query: Q) -> bool + where + Self: Sized, + Self::Item: PartialEq, + { + self.any(|x| x == query) + } + /// Check whether all elements compare equal. /// /// Empty iterators are considered to have equal elements: From 29b68288dd443b65917646d9e327865de4cd79d3 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 13:52:20 -0500 Subject: [PATCH 2/9] Update src/lib.rs Co-authored-by: Jack Wrenn --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8edfbe0fa..17d755d6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1608,11 +1608,12 @@ pub trait Itertools : Iterator { None } - /// Check whether the iterator contains an item. + /// Returns `true` if the given item is present in this iterator. /// - /// If the iterator contains the item prior to its end, - /// this method will short-circuit and only partially - /// exhaust the iterator. + /// This method is short-circuiting. If the given item is present in this + /// iterator, the this method will consume the iterator up-to-and-including + /// the item. If the given item is not present in this iterator, the + /// iterator will be exhausted. /// /// ``` /// use itertools::Itertools; From 3c4d78475ebe8cb3b345251372f7c7363e9f38df Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 13:52:49 -0500 Subject: [PATCH 3/9] Update src/lib.rs Co-authored-by: Jack Wrenn --- src/lib.rs | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 17d755d6f..f8c8c58dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1618,35 +1618,20 @@ pub trait Itertools : Iterator { /// ``` /// use itertools::Itertools; /// - /// let data = vec![ - /// "foo".to_owned(), - /// "bar".to_owned(), - /// "baz".to_owned(), - /// ]; - /// // this could get expensive: - /// assert!(data.contains(&"foo".to_owned())); - /// // this, less so: - /// assert!(data.iter().contains("foo")); - /// - /// // now the not-as-motivating tests involving Copy data: - /// - /// let data = vec![4usize, 5, 1, 3, 0, 2]; - /// assert!(data.iter().contains(&4)); - /// assert!(!data.iter().contains(&6)); - /// for x in (0..50) { - /// assert_eq!(data.contains(&x), data.iter().contains(&x)); - /// } - /// - /// let mut it = data.iter(); - /// assert!(!it.contains(&6)); - /// assert_eq!(it.next(), None); - /// - /// let mut it = data.iter(); - /// assert!(it.contains(&3)); - /// assert_eq!(it.next(), Some(&0)); - /// - /// let data : Option = None; - /// assert!(!data.into_iter().contains(0)); + /// #[derive(PartialEq, Debug)] + /// enum Enum { A, B, C, D, E, } + /// + /// let mut iter = vec![Enum::A, Enum::B, Enum::C, Enum::D].into_iter(); + /// + /// // search `iter` for `B` + /// assert_eq!(iter.contains(Enum::B), true); + /// // `B` was found, so the iterator now rests at the item after `B` (i.e, `C`). + /// assert_eq!(iter.next(), Some(Enum::C)); + /// + /// // search `iter` for `E` + /// assert_eq!(iter.contains(Enum::E), false); + /// // `E` wasn't found, so `iter` is now exhausted + /// assert_eq!(iter.next(), None); /// ``` fn contains(&mut self, query: Q) -> bool where From d4a2937237d04266cef7b890fbc16be0d37b4715 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 13:57:35 -0500 Subject: [PATCH 4/9] Update src/lib.rs Co-authored-by: Jack Wrenn --- src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f8c8c58dd..f1716f4aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1616,8 +1616,6 @@ pub trait Itertools : Iterator { /// iterator will be exhausted. /// /// ``` - /// use itertools::Itertools; - /// /// #[derive(PartialEq, Debug)] /// enum Enum { A, B, C, D, E, } /// @@ -1633,12 +1631,13 @@ pub trait Itertools : Iterator { /// // `E` wasn't found, so `iter` is now exhausted /// assert_eq!(iter.next(), None); /// ``` - fn contains(&mut self, query: Q) -> bool + fn contains(&mut self, query: &Q) -> bool where Self: Sized, - Self::Item: PartialEq, + Self::Item: Borrow, + Q: PartialEq, { - self.any(|x| x == query) + self.any(|x| x.borrow() == query) } /// Check whether all elements compare equal. From 43911579d995ebab2ea5f384b29c4c400161c47c Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 13:59:04 -0500 Subject: [PATCH 5/9] Update src/lib.rs --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f1716f4aa..42ec95f5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1611,7 +1611,7 @@ pub trait Itertools : Iterator { /// Returns `true` if the given item is present in this iterator. /// /// This method is short-circuiting. If the given item is present in this - /// iterator, the this method will consume the iterator up-to-and-including + /// iterator, this method will consume the iterator up-to-and-including /// the item. If the given item is not present in this iterator, the /// iterator will be exhausted. /// From eb967a1a0b5765fd76891fb285ad18a3cbe7f3e2 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 14:11:13 -0500 Subject: [PATCH 6/9] Fix missing import --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 42ec95f5e..74e165500 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,6 +59,8 @@ use alloc::{ pub use either::Either; +#[cfg(feature = "use_std")] +use std::borrow::Borrow; #[cfg(feature = "use_std")] use std::collections::HashMap; use std::iter::{IntoIterator, once}; From 2541ca928802492714ac27ac812390a3b6215df3 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 14:21:56 -0500 Subject: [PATCH 7/9] Make the doctests pass --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 74e165500..ae2683e5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1618,18 +1618,20 @@ pub trait Itertools : Iterator { /// iterator will be exhausted. /// /// ``` + /// use itertools::Itertools; + /// /// #[derive(PartialEq, Debug)] /// enum Enum { A, B, C, D, E, } /// /// let mut iter = vec![Enum::A, Enum::B, Enum::C, Enum::D].into_iter(); /// /// // search `iter` for `B` - /// assert_eq!(iter.contains(Enum::B), true); + /// assert_eq!(iter.contains(&Enum::B), true); /// // `B` was found, so the iterator now rests at the item after `B` (i.e, `C`). /// assert_eq!(iter.next(), Some(Enum::C)); /// /// // search `iter` for `E` - /// assert_eq!(iter.contains(Enum::E), false); + /// assert_eq!(iter.contains(&Enum::E), false); /// // `E` wasn't found, so `iter` is now exhausted /// assert_eq!(iter.next(), None); /// ``` From a15e7d9d54455750bf70a909119dfb42f42260c7 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 14:33:28 -0500 Subject: [PATCH 8/9] Update src/lib.rs Co-authored-by: Jack Wrenn --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae2683e5d..a9916ad72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,8 +59,7 @@ use alloc::{ pub use either::Either; -#[cfg(feature = "use_std")] -use std::borrow::Borrow; +use core::borrow:Borrow; #[cfg(feature = "use_std")] use std::collections::HashMap; use std::iter::{IntoIterator, once}; From 6a8a6c16b2e1dd013bc0ec9409b753a20f5852b7 Mon Sep 17 00:00:00 2001 From: Milo Mirate Date: Sun, 10 Jan 2021 14:35:55 -0500 Subject: [PATCH 9/9] Fix typo --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a9916ad72..fee1e6108 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,7 +59,7 @@ use alloc::{ pub use either::Either; -use core::borrow:Borrow; +use core::borrow::Borrow; #[cfg(feature = "use_std")] use std::collections::HashMap; use std::iter::{IntoIterator, once};