From ba0ddf281bad63cc01a07a4a25e44f75c2330e4d Mon Sep 17 00:00:00 2001 From: Abhik Jain Date: Sat, 1 Jul 2023 15:17:36 +0530 Subject: [PATCH] fix: take_while_inclusive takes iterator by value this helps in case TakeWhileInclusive is returned from a function, as otherwise we would be returning a struct which takes a reference from a locally created iterator. --- src/lib.rs | 16 ++++++++-------- src/take_while_inclusive.rs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c23a65db5..c9bad16eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1519,7 +1519,7 @@ pub trait Itertools : Iterator { /// .collect(); /// let expected: Vec<_> = vec![1, 2, 3].into_iter().map(NoCloneImpl).collect(); /// assert_eq!(filtered, expected); - fn take_while_inclusive(&mut self, accept: F) -> TakeWhileInclusive + fn take_while_inclusive(self, accept: F) -> TakeWhileInclusive where Self: Sized, F: FnMut(&Self::Item) -> bool, @@ -2650,7 +2650,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_unstable`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is unstable (i.e., may reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2681,7 +2681,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_unstable_by`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is unstable (i.e., may reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2716,7 +2716,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_unstable_by_key`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is unstable (i.e., may reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2752,7 +2752,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is stable (i.e., does not reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2783,7 +2783,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_by`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is stable (i.e., does not reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2818,7 +2818,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_by_key`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is stable (i.e., does not reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted @@ -2855,7 +2855,7 @@ pub trait Itertools : Iterator { /// **Note:** This consumes the entire iterator, uses the /// [`slice::sort_by_cached_key`] method and returns the result as a new /// iterator that owns its elements. - /// + /// /// This sort is stable (i.e., does not reorder equal elements). /// /// The sorted iterator, if directly collected to a `Vec`, is converted diff --git a/src/take_while_inclusive.rs b/src/take_while_inclusive.rs index e2a7479e0..5ef1953d2 100644 --- a/src/take_while_inclusive.rs +++ b/src/take_while_inclusive.rs @@ -8,30 +8,30 @@ use std::fmt; /// See [`.take_while_inclusive()`](crate::Itertools::take_while_inclusive) /// for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct TakeWhileInclusive<'a, I: 'a, F> { - iter: &'a mut I, +pub struct TakeWhileInclusive { + iter: I, predicate: F, done: bool, } -impl<'a, I, F> TakeWhileInclusive<'a, I, F> +impl TakeWhileInclusive where I: Iterator, F: FnMut(&I::Item) -> bool, { /// Create a new [`TakeWhileInclusive`] from an iterator and a predicate. - pub fn new(iter: &'a mut I, predicate: F) -> Self { + pub fn new(iter: I, predicate: F) -> Self { Self { iter, predicate, done: false} } } -impl<'a, I, F> fmt::Debug for TakeWhileInclusive<'a, I, F> +impl fmt::Debug for TakeWhileInclusive where I: Iterator + fmt::Debug, { debug_fmt_fields!(TakeWhileInclusive, iter); } -impl<'a, I, F> Iterator for TakeWhileInclusive<'a, I, F> +impl Iterator for TakeWhileInclusive where I: Iterator, F: FnMut(&I::Item) -> bool @@ -60,9 +60,9 @@ where } } -impl FusedIterator for TakeWhileInclusive<'_, I, F> +impl FusedIterator for TakeWhileInclusive where I: Iterator, F: FnMut(&I::Item) -> bool { -} \ No newline at end of file +}