Skip to content

Commit

Permalink
fix: take_while_inclusive takes iterator by value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abhikjain360 committed Jul 1, 2023
1 parent 62a6401 commit ba0ddf2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&mut self, accept: F) -> TakeWhileInclusive<Self, F>
fn take_while_inclusive<F>(self, accept: F) -> TakeWhileInclusive<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/take_while_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, F> {
iter: I,
predicate: F,
done: bool,
}

impl<'a, I, F> TakeWhileInclusive<'a, I, F>
impl<I, F> TakeWhileInclusive<I, F>
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<I, F> fmt::Debug for TakeWhileInclusive<I, F>
where I: Iterator + fmt::Debug,
{
debug_fmt_fields!(TakeWhileInclusive, iter);
}

impl<'a, I, F> Iterator for TakeWhileInclusive<'a, I, F>
impl<I, F> Iterator for TakeWhileInclusive<I, F>
where
I: Iterator,
F: FnMut(&I::Item) -> bool
Expand Down Expand Up @@ -60,9 +60,9 @@ where
}
}

impl<I, F> FusedIterator for TakeWhileInclusive<'_, I, F>
impl<I, F> FusedIterator for TakeWhileInclusive<I, F>
where
I: Iterator,
F: FnMut(&I::Item) -> bool
{
}
}

0 comments on commit ba0ddf2

Please sign in to comment.