From de354f4a0c5ca736e2324c1ada10490e843d2086 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Fri, 3 Jun 2022 08:58:42 +0200 Subject: [PATCH 01/11] fix clippy::while_let_on_iterator --- src/lib.rs | 7 +------ src/unique_impl.rs | 5 +++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 211827ff2..db8bca4be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1910,12 +1910,7 @@ pub trait Itertools: Iterator { where P: FnMut(&Self::Item) -> bool, { - for (index, elt) in self.enumerate() { - if pred(&elt) { - return Some((index, elt)); - } - } - None + self.enumerate().find(|x| pred(&x.1)) } /// Find the value of the first element satisfying a predicate or return the last element, if any. /// diff --git a/src/unique_impl.rs b/src/unique_impl.rs index 0f6397e48..268abe277 100644 --- a/src/unique_impl.rs +++ b/src/unique_impl.rs @@ -110,9 +110,10 @@ where if let Entry::Vacant(entry) = used.entry(v) { let elt = entry.key().clone(); entry.insert(()); - return Some(elt); + Some(elt) + } else { + None } - None }) } From 4dffdd90992cf9c61f85a3096654ba43097c1050 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Fri, 3 Jun 2022 09:07:08 +0200 Subject: [PATCH 02/11] allow clippy::unnecessary_lazy_evaluations --- src/tuple_impl.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index 4a7cd4da8..5e061bde5 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -54,6 +54,7 @@ where } } + #[allow(clippy::unnecessary_lazy_evaluations)] // buffer.len is more complex than usual fn size_hint(&self) -> (usize, Option) { let buffer = &self.buf.as_ref()[self.cur..]; let len = if buffer.is_empty() { From d484340f4ea8ba6f663325e312e111808162f0dd Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Fri, 3 Jun 2022 09:35:53 +0200 Subject: [PATCH 03/11] fix clippy::derive_partial_eq_without_eq --- src/minmax.rs | 2 +- tests/test_core.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/minmax.rs b/src/minmax.rs index f04e5adba..9f02a49f9 100644 --- a/src/minmax.rs +++ b/src/minmax.rs @@ -1,7 +1,7 @@ /// `MinMaxResult` is an enum returned by `minmax`. /// /// See [`.minmax()`](crate::Itertools::minmax) for more detail. -#[derive(Copy, Clone, PartialEq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum MinMaxResult { /// Empty iterator NoElements, diff --git a/tests/test_core.rs b/tests/test_core.rs index fc2c858a3..86625c8cf 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -228,7 +228,7 @@ fn count_clones() { // Check that RepeatN only clones N - 1 times. use core::cell::Cell; - #[derive(PartialEq, Debug)] + #[derive(PartialEq, Eq, Debug)] struct Foo { n: Cell, } From e43de24af01e37a4771696a6a3ef79fd2cbecf64 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 2 Jun 2022 13:57:22 +0200 Subject: [PATCH 04/11] add rustdoc and clippy to githubs workflow --- .github/workflows/ci.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10b9bb9d9..1b95b91e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,13 @@ jobs: components: clippy - run: RUSTFLAGS="--deny warnings" cargo clippy ${{ matrix.features }} + doc: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - run: RUSTDOCFLAGS="-Dwarnings" cargo doc --all-features + msrv: runs-on: ubuntu-latest env: From bc7540fa6bcfcfa4a9614ea44a422591f86de73a Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 10:23:37 +0200 Subject: [PATCH 05/11] fix clippy::type_repetition_in_bounds --- src/adaptors/coalesce.rs | 5 +++-- src/free.rs | 4 ++-- src/groupbylazy.rs | 24 ++++++++++++++---------- src/lib.rs | 5 ++--- src/peeking_take_while.rs | 8 ++++---- src/tuple_impl.rs | 2 +- src/ziptuple.rs | 3 +-- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/adaptors/coalesce.rs b/src/adaptors/coalesce.rs index 21eaa016d..ad9df1192 100644 --- a/src/adaptors/coalesce.rs +++ b/src/adaptors/coalesce.rs @@ -17,9 +17,10 @@ where f: F, } -impl Clone for CoalesceBy +impl Clone for CoalesceBy where - I: Iterator, + I: Clone + Iterator, + F: Clone, C: CountItem, C::CItem: Clone, { diff --git a/src/free.rs b/src/free.rs index 5ce1b49b3..617a1977d 100644 --- a/src/free.rs +++ b/src/free.rs @@ -166,10 +166,10 @@ where /// /// assert_eq!(cloned(b"abc").next(), Some(b'a')); /// ``` -pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned +pub fn cloned<'a, I, T>(iterable: I) -> iter::Cloned where I: IntoIterator, - T: Clone, + T: Clone + 'a, { iterable.into_iter().cloned() } diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index 6cf33838c..e4b240548 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -7,9 +7,9 @@ trait KeyFunction { fn call_mut(&mut self, arg: A) -> Self::Key; } -impl KeyFunction for F +impl KeyFunction for F where - F: FnMut(A) -> K, + F: FnMut(A) -> K + ?Sized, { type Key = K; #[inline] @@ -370,10 +370,12 @@ where /// /// See [`.group_by()`](crate::Itertools::group_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct Groups<'a, K: 'a, I: 'a, F: 'a> +pub struct Groups<'a, K, I, F> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, + K: 'a, + F: 'a, { parent: &'a GroupBy, } @@ -409,10 +411,12 @@ where /// An iterator for the elements in a single group. /// /// Iterator element type is `I::Item`. -pub struct Group<'a, K: 'a, I: 'a, F: 'a> +pub struct Group<'a, K, I, F> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, + K: 'a, + F: 'a, { parent: &'a GroupBy, index: usize, @@ -537,9 +541,9 @@ where /// See [`.chunks()`](crate::Itertools::chunks) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Clone)] -pub struct Chunks<'a, I: 'a> +pub struct Chunks<'a, I> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, { parent: &'a IntoChunks, @@ -568,9 +572,9 @@ where /// An iterator for the elements in a single chunk. /// /// Iterator element type is `I::Item`. -pub struct Chunk<'a, I: 'a> +pub struct Chunk<'a, I> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, { parent: &'a IntoChunks, diff --git a/src/lib.rs b/src/lib.rs index db8bca4be..5845b9496 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2124,8 +2124,7 @@ pub trait Itertools: Iterator { /// ``` fn dropping_back(mut self, n: usize) -> Self where - Self: Sized, - Self: DoubleEndedIterator, + Self: Sized + DoubleEndedIterator, { if n > 0 { (&mut self).rev().nth(n - 1); @@ -3962,7 +3961,7 @@ pub trait Itertools: Iterator { } } -impl Itertools for T where T: Iterator {} +impl Itertools for T where T: Iterator + ?Sized {} /// Return `true` if both iterables produce equal sequences /// (elements pairwise equal and sequences of the same length), diff --git a/src/peeking_take_while.rs b/src/peeking_take_while.rs index b08794a8d..b4852c1c9 100644 --- a/src/peeking_take_while.rs +++ b/src/peeking_take_while.rs @@ -96,17 +96,17 @@ where /// See [`.peeking_take_while()`](crate::Itertools::peeking_take_while) /// for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct PeekingTakeWhile<'a, I: 'a, F> +pub struct PeekingTakeWhile<'a, I, F> where - I: Iterator, + I: Iterator + 'a, { iter: &'a mut I, f: F, } -impl<'a, I: 'a, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F> +impl<'a, I, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F> where - I: Iterator + std::fmt::Debug, + I: Iterator + std::fmt::Debug + 'a, { debug_fmt_fields!(PeekingTakeWhile, iter); } diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index 5e061bde5..f65941965 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -245,7 +245,7 @@ where /// information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Debug, Clone)] -pub struct CircularTupleWindows +pub struct CircularTupleWindows where I: Iterator + Clone, T: TupleCollect + Clone, diff --git a/src/ziptuple.rs b/src/ziptuple.rs index 82760ae8f..5ff0fad33 100644 --- a/src/ziptuple.rs +++ b/src/ziptuple.rs @@ -39,8 +39,7 @@ pub struct Zip { /// [`izip!()`]: crate::izip pub fn multizip(t: U) -> Zip where - Zip: From, - Zip: Iterator, + Zip: From + Iterator, { Zip::from(t) } From e663b7a3a3c27c93ba0a5bc364efdc210dfc487e Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 10:40:04 +0200 Subject: [PATCH 06/11] fix clippy::if_not_else --- src/combinations_with_replacement.rs | 10 +++++----- src/minmax.rs | 20 ++++++++++---------- src/with_position.rs | 16 ++++++++-------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/combinations_with_replacement.rs b/src/combinations_with_replacement.rs index 88d858b5f..9b7ea6d1a 100644 --- a/src/combinations_with_replacement.rs +++ b/src/combinations_with_replacement.rs @@ -65,13 +65,13 @@ where fn next(&mut self) -> Option { // If this is the first iteration, return early if self.first { - // In empty edge cases, stop iterating immediately - return if !(self.indices.is_empty() || self.pool.get_next()) { - None - // Otherwise, yield the initial state - } else { + return if self.indices.is_empty() || self.pool.get_next() { + // yield the initial state self.first = false; Some(self.current()) + } else { + // In empty edge cases, stop iterating immediately + None }; } diff --git a/src/minmax.rs b/src/minmax.rs index 9f02a49f9..e522bcb4f 100644 --- a/src/minmax.rs +++ b/src/minmax.rs @@ -91,16 +91,7 @@ where }; let first_key = key_for(&first); let second_key = key_for(&second); - if !lt(&second, &first, &second_key, &first_key) { - if lt(&first, &min, &first_key, &min_key) { - min = first; - min_key = first_key; - } - if !lt(&second, &max, &second_key, &max_key) { - max = second; - max_key = second_key; - } - } else { + if lt(&second, &first, &second_key, &first_key) { if lt(&second, &min, &second_key, &min_key) { min = second; min_key = second_key; @@ -109,6 +100,15 @@ where max = first; max_key = first_key; } + } else { + if lt(&first, &min, &first_key, &min_key) { + min = first; + min_key = first_key; + } + if !lt(&second, &max, &second_key, &max_key) { + max = second; + max_key = second_key; + } } } diff --git a/src/with_position.rs b/src/with_position.rs index 89cddeb8a..f8423f98a 100644 --- a/src/with_position.rs +++ b/src/with_position.rs @@ -55,7 +55,14 @@ impl Iterator for WithPosition { fn next(&mut self) -> Option { match self.peekable.next() { Some(item) => { - if !self.handled_first { + if self.handled_first { + // Have seen the first item, and there's something left. + // Peek to see if this is the last item. + match self.peekable.peek() { + Some(_) => Some((Position::Middle, item)), + None => Some((Position::Last, item)), + } + } else { // Haven't seen the first item yet, and there is one to give. self.handled_first = true; // Peek to see if this is also the last item, @@ -64,13 +71,6 @@ impl Iterator for WithPosition { Some(_) => Some((Position::First, item)), None => Some((Position::Only, item)), } - } else { - // Have seen the first item, and there's something left. - // Peek to see if this is the last item. - match self.peekable.peek() { - Some(_) => Some((Position::Middle, item)), - None => Some((Position::Last, item)), - } } } // Iterator is finished. From c69ef49309aba4c9eac564bf40442d30860cc9f7 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 10:45:35 +0200 Subject: [PATCH 07/11] fix clippy::enum_glob_use --- src/adaptors/multi_product.rs | 12 ++-- src/either_or_both.rs | 132 +++++++++++++++++----------------- 2 files changed, 70 insertions(+), 74 deletions(-) diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index f02f8c740..c2893b4ad 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -77,16 +77,14 @@ where multi_iters: &mut [MultiProductIter], mut state: MultiProductIterState, ) -> bool { - use self::MultiProductIterState::*; - if let Some((last, rest)) = multi_iters.split_last_mut() { let on_first_iter = match state { - StartOfIter => { + MultiProductIterState::StartOfIter => { let on_first_iter = !last.in_progress(); - state = MidIter { on_first_iter }; + state = MultiProductIterState::MidIter { on_first_iter }; on_first_iter } - MidIter { on_first_iter } => on_first_iter, + MultiProductIterState::MidIter { on_first_iter } => on_first_iter, }; if !on_first_iter { @@ -108,8 +106,8 @@ where // Reached end of iterator list. On initialisation, return true. // At end of iteration (final iterator finishes), finish. match state { - StartOfIter => false, - MidIter { on_first_iter } => on_first_iter, + MultiProductIterState::StartOfIter => false, + MultiProductIterState::MidIter { on_first_iter } => on_first_iter, } } } diff --git a/src/either_or_both.rs b/src/either_or_both.rs index 2232cd68c..b504d69f7 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -1,7 +1,5 @@ use core::ops::{Deref, DerefMut}; -use crate::EitherOrBoth::*; - use either::Either; /// Value that either holds a single A or B, or both. @@ -29,13 +27,13 @@ impl EitherOrBoth { /// If `Left`, return true. Otherwise, return false. /// Exclusive version of [`has_left`](EitherOrBoth::has_left). pub fn is_left(&self) -> bool { - matches!(self, Left(_)) + matches!(self, EitherOrBoth::Left(_)) } /// If `Right`, return true. Otherwise, return false. /// Exclusive version of [`has_right`](EitherOrBoth::has_right). pub fn is_right(&self) -> bool { - matches!(self, Right(_)) + matches!(self, EitherOrBoth::Right(_)) } /// If `Both`, return true. Otherwise, return false. @@ -46,16 +44,16 @@ impl EitherOrBoth { /// If `Left`, or `Both`, return `Some` with the left value. Otherwise, return `None`. pub fn left(self) -> Option { match self { - Left(left) | Both(left, _) => Some(left), - _ => None, + EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => Some(left), + EitherOrBoth::Right(_) => None, } } /// If `Right`, or `Both`, return `Some` with the right value. Otherwise, return `None`. pub fn right(self) -> Option { match self { - Right(right) | Both(_, right) => Some(right), - _ => None, + EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => Some(right), + EitherOrBoth::Left(_) => None, } } @@ -87,7 +85,7 @@ impl EitherOrBoth { /// ``` pub fn just_left(self) -> Option { match self { - Left(left) => Some(left), + EitherOrBoth::Left(left) => Some(left), _ => None, } } @@ -112,7 +110,7 @@ impl EitherOrBoth { /// ``` pub fn just_right(self) -> Option { match self { - Right(right) => Some(right), + EitherOrBoth::Right(right) => Some(right), _ => None, } } @@ -120,7 +118,7 @@ impl EitherOrBoth { /// If `Both`, return `Some` containing the left and right values. Otherwise, return `None`. pub fn both(self) -> Option<(A, B)> { match self { - Both(a, b) => Some((a, b)), + EitherOrBoth::Both(a, b) => Some((a, b)), _ => None, } } @@ -131,8 +129,8 @@ impl EitherOrBoth { B: Into, { match self { - Left(a) | Both(a, _) => a, - Right(b) => b.into(), + EitherOrBoth::Left(a) | EitherOrBoth::Both(a, _) => a, + EitherOrBoth::Right(b) => b.into(), } } @@ -142,26 +140,26 @@ impl EitherOrBoth { A: Into, { match self { - Right(b) | Both(_, b) => b, - Left(a) => a.into(), + EitherOrBoth::Right(b) | EitherOrBoth::Both(_, b) => b, + EitherOrBoth::Left(a) => a.into(), } } /// Converts from `&EitherOrBoth` to `EitherOrBoth<&A, &B>`. pub fn as_ref(&self) -> EitherOrBoth<&A, &B> { match *self { - Left(ref left) => Left(left), - Right(ref right) => Right(right), - Both(ref left, ref right) => Both(left, right), + EitherOrBoth::Left(ref left) => EitherOrBoth::Left(left), + EitherOrBoth::Right(ref right) => EitherOrBoth::Right(right), + EitherOrBoth::Both(ref left, ref right) => EitherOrBoth::Both(left, right), } } /// Converts from `&mut EitherOrBoth` to `EitherOrBoth<&mut A, &mut B>`. pub fn as_mut(&mut self) -> EitherOrBoth<&mut A, &mut B> { match *self { - Left(ref mut left) => Left(left), - Right(ref mut right) => Right(right), - Both(ref mut left, ref mut right) => Both(left, right), + EitherOrBoth::Left(ref mut left) => EitherOrBoth::Left(left), + EitherOrBoth::Right(ref mut right) => EitherOrBoth::Right(right), + EitherOrBoth::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), } } @@ -172,9 +170,9 @@ impl EitherOrBoth { B: Deref, { match *self { - Left(ref left) => Left(left), - Right(ref right) => Right(right), - Both(ref left, ref right) => Both(left, right), + EitherOrBoth::Left(ref left) => EitherOrBoth::Left(left), + EitherOrBoth::Right(ref right) => EitherOrBoth::Right(right), + EitherOrBoth::Both(ref left, ref right) => EitherOrBoth::Both(left, right), } } @@ -185,18 +183,18 @@ impl EitherOrBoth { B: DerefMut, { match *self { - Left(ref mut left) => Left(left), - Right(ref mut right) => Right(right), - Both(ref mut left, ref mut right) => Both(left, right), + EitherOrBoth::Left(ref mut left) => EitherOrBoth::Left(left), + EitherOrBoth::Right(ref mut right) => EitherOrBoth::Right(right), + EitherOrBoth::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), } } /// Convert `EitherOrBoth` to `EitherOrBoth`. pub fn flip(self) -> EitherOrBoth { match self { - Left(a) => Right(a), - Right(b) => Left(b), - Both(a, b) => Both(b, a), + EitherOrBoth::Left(a) => EitherOrBoth::Right(a), + EitherOrBoth::Right(b) => EitherOrBoth::Left(b), + EitherOrBoth::Both(a, b) => EitherOrBoth::Both(b, a), } } @@ -207,9 +205,9 @@ impl EitherOrBoth { F: FnOnce(A) -> M, { match self { - Both(a, b) => Both(f(a), b), - Left(a) => Left(f(a)), - Right(b) => Right(b), + EitherOrBoth::Both(a, b) => EitherOrBoth::Both(f(a), b), + EitherOrBoth::Left(a) => EitherOrBoth::Left(f(a)), + EitherOrBoth::Right(b) => EitherOrBoth::Right(b), } } @@ -220,9 +218,9 @@ impl EitherOrBoth { F: FnOnce(B) -> M, { match self { - Left(a) => Left(a), - Right(b) => Right(f(b)), - Both(a, b) => Both(a, f(b)), + EitherOrBoth::Left(a) => EitherOrBoth::Left(a), + EitherOrBoth::Right(b) => EitherOrBoth::Right(f(b)), + EitherOrBoth::Both(a, b) => EitherOrBoth::Both(a, f(b)), } } @@ -235,9 +233,9 @@ impl EitherOrBoth { G: FnOnce(B) -> R, { match self { - Left(a) => Left(f(a)), - Right(b) => Right(g(b)), - Both(a, b) => Both(f(a), g(b)), + EitherOrBoth::Left(a) => EitherOrBoth::Left(f(a)), + EitherOrBoth::Right(b) => EitherOrBoth::Right(g(b)), + EitherOrBoth::Both(a, b) => EitherOrBoth::Both(f(a), g(b)), } } @@ -248,8 +246,8 @@ impl EitherOrBoth { F: FnOnce(A) -> EitherOrBoth, { match self { - Left(a) | Both(a, _) => f(a), - Right(b) => Right(b), + EitherOrBoth::Left(a) | EitherOrBoth::Both(a, _) => f(a), + EitherOrBoth::Right(b) => EitherOrBoth::Right(b), } } @@ -260,8 +258,8 @@ impl EitherOrBoth { F: FnOnce(B) -> EitherOrBoth, { match self { - Left(a) => Left(a), - Right(b) | Both(_, b) => f(b), + EitherOrBoth::Left(a) => EitherOrBoth::Left(a), + EitherOrBoth::Right(b) | EitherOrBoth::Both(_, b) => f(b), } } @@ -286,9 +284,9 @@ impl EitherOrBoth { /// ``` pub fn or(self, l: A, r: B) -> (A, B) { match self { - Left(inner_l) => (inner_l, r), - Right(inner_r) => (l, inner_r), - Both(inner_l, inner_r) => (inner_l, inner_r), + EitherOrBoth::Left(inner_l) => (inner_l, r), + EitherOrBoth::Right(inner_r) => (l, inner_r), + EitherOrBoth::Both(inner_l, inner_r) => (inner_l, inner_r), } } @@ -323,9 +321,9 @@ impl EitherOrBoth { /// ``` pub fn or_else A, R: FnOnce() -> B>(self, l: L, r: R) -> (A, B) { match self { - Left(inner_l) => (inner_l, r()), - Right(inner_r) => (l(), inner_r), - Both(inner_l, inner_r) => (inner_l, inner_r), + EitherOrBoth::Left(inner_l) => (inner_l, r()), + EitherOrBoth::Right(inner_r) => (l(), inner_r), + EitherOrBoth::Both(inner_l, inner_r) => (inner_l, inner_r), } } @@ -348,8 +346,8 @@ impl EitherOrBoth { F: FnOnce() -> A, { match self { - Left(left) | Both(left, _) => left, - Right(_) => self.insert_left(f()), + EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => left, + EitherOrBoth::Right(_) => self.insert_left(f()), } } @@ -360,8 +358,8 @@ impl EitherOrBoth { F: FnOnce() -> B, { match self { - Right(right) | Both(_, right) => right, - Left(_) => self.insert_right(f()), + EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => right, + EitherOrBoth::Left(_) => self.insert_right(f()), } } @@ -383,21 +381,21 @@ impl EitherOrBoth { /// ``` pub fn insert_left(&mut self, val: A) -> &mut A { match self { - Left(left) | Both(left, _) => { + EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => { *left = val; left } - Right(right) => { + EitherOrBoth::Right(right) => { // This is like a map in place operation. We move out of the reference, // change the value, and then move back into the reference. unsafe { // SAFETY: We know this pointer is valid for reading since we got it from a reference. let right = std::ptr::read(right as *mut _); // SAFETY: Again, we know the pointer is valid since we got it from a reference. - std::ptr::write(self as *mut _, Both(val, right)); + std::ptr::write(self as *mut _, EitherOrBoth::Both(val, right)); } - if let Both(left, _) = self { + if let EitherOrBoth::Both(left, _) = self { left } else { // SAFETY: The above pattern will always match, since we just @@ -425,20 +423,20 @@ impl EitherOrBoth { /// ``` pub fn insert_right(&mut self, val: B) -> &mut B { match self { - Right(right) | Both(_, right) => { + EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => { *right = val; right } - Left(left) => { + EitherOrBoth::Left(left) => { // This is like a map in place operation. We move out of the reference, // change the value, and then move back into the reference. unsafe { // SAFETY: We know this pointer is valid for reading since we got it from a reference. let left = std::ptr::read(left as *mut _); // SAFETY: Again, we know the pointer is valid since we got it from a reference. - std::ptr::write(self as *mut _, Both(left, val)); + std::ptr::write(self as *mut _, EitherOrBoth::Both(left, val)); } - if let Both(_, right) = self { + if let EitherOrBoth::Both(_, right) = self { right } else { // SAFETY: The above pattern will always match, since we just @@ -452,8 +450,8 @@ impl EitherOrBoth { /// Set `self` to `Both(..)`, containing the specified left and right values, /// and returns a mutable reference to those values. pub fn insert_both(&mut self, left: A, right: B) -> (&mut A, &mut B) { - *self = Both(left, right); - if let Both(left, right) = self { + *self = EitherOrBoth::Both(left, right); + if let EitherOrBoth::Both(left, right) = self { (left, right) } else { // SAFETY: The above pattern will always match, since we just @@ -487,9 +485,9 @@ impl EitherOrBoth { F: FnOnce(T, T) -> T, { match self { - Left(a) => a, - Right(b) => b, - Both(a, b) => f(a, b), + EitherOrBoth::Left(a) => a, + EitherOrBoth::Right(b) => b, + EitherOrBoth::Both(a, b) => f(a, b), } } } @@ -500,7 +498,7 @@ impl Into>> for EitherOrBoth { match self { EitherOrBoth::Left(l) => Some(Either::Left(l)), EitherOrBoth::Right(r) => Some(Either::Right(r)), - _ => None, + EitherOrBoth::Both(..) => None, } } } From bb04042490dd140a8fb9beb9ce54810ce4b7685c Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 10:51:49 +0200 Subject: [PATCH 08/11] fix clippy::option_if_let_else --- src/adaptors/multi_product.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index c2893b4ad..6873b5354 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -123,11 +123,7 @@ where /// Returns true if iteration has started and has not yet finished; false /// otherwise. fn in_progress(&self) -> bool { - if let Some(last) = self.0.last() { - last.in_progress() - } else { - false - } + self.0.last().map_or(false, |last| last.in_progress()) } } From ae943dac7afa9b604a0ffb8b46d17e3c5e255427 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 11:10:34 +0200 Subject: [PATCH 09/11] fix some more clippy lints --- src/adaptors/multi_product.rs | 2 +- src/flatten_ok.rs | 3 +-- src/groupbylazy.rs | 2 +- src/kmerge_impl.rs | 2 +- src/unique_impl.rs | 9 ++++++--- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index 6873b5354..3782f5990 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -123,7 +123,7 @@ where /// Returns true if iteration has started and has not yet finished; false /// otherwise. fn in_progress(&self) -> bool { - self.0.last().map_or(false, |last| last.in_progress()) + self.0.last().map_or(false, MultiProductIter::in_progress) } } diff --git a/src/flatten_ok.rs b/src/flatten_ok.rs index 21ae1f722..f431c1325 100644 --- a/src/flatten_ok.rs +++ b/src/flatten_ok.rs @@ -76,8 +76,7 @@ where let inner_hint = |inner: &Option| { inner .as_ref() - .map(Iterator::size_hint) - .unwrap_or((0, Some(0))) + .map_or((0, Some(0)), Iterator::size_hint) }; let inner_front = inner_hint(&self.inner_front); let inner_back = inner_hint(&self.inner_back); diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index e4b240548..6279899d0 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -112,7 +112,7 @@ where if client < self.oldest_buffered_group { return None; } - let elt = self.buffer.get_mut(bufidx).and_then(|queue| queue.next()); + let elt = self.buffer.get_mut(bufidx).and_then(Iterator::next); if elt.is_none() && client == self.oldest_buffered_group { // FIXME: VecDeque is unfortunately not zero allocation when empty, // so we do this job manually. diff --git a/src/kmerge_impl.rs b/src/kmerge_impl.rs index c077cdda1..af1462679 100644 --- a/src/kmerge_impl.rs +++ b/src/kmerge_impl.rs @@ -226,7 +226,7 @@ where #[allow(deprecated)] //TODO: once msrv hits 1.51. replace `fold1` with `reduce` self.heap .iter() - .map(|i| i.size_hint()) + .map(HeadTail::size_hint) .fold1(size_hint::add) .unwrap_or((0, Some(0))) } diff --git a/src/unique_impl.rs b/src/unique_impl.rs index 268abe277..ec573f96f 100644 --- a/src/unique_impl.rs +++ b/src/unique_impl.rs @@ -1,3 +1,8 @@ +// Use a Hashmap for the entry API in order to prevent hashing twice. +// This can maybe be replaced with e a HashSet once `get_or_insert_with` +// // or a proper Entry API for Hashset is stable and meets the msrv +#![allow(clippy::zero_sized_map_values)] + use std::collections::hash_map::Entry; use std::collections::HashMap; use std::fmt; @@ -11,9 +16,7 @@ use std::iter::FusedIterator; #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct UniqueBy { iter: I, - // Use a Hashmap for the Entry API in order to prevent hashing twice. - // This can maybe be replaced with a HashSet once `get_or_insert_with` - // or a proper Entry API for Hashset is stable and meets this msrv + // see comment for `allow(clippy::zero_sized_map_values)` for reasoning used: HashMap, f: F, } From 09effd55ed1fa47ae280fb20d3743d23b29c4ef4 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Wed, 10 Jan 2024 13:30:22 +0100 Subject: [PATCH 10/11] run rustfmt --- src/flatten_ok.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/flatten_ok.rs b/src/flatten_ok.rs index f431c1325..ab86e910d 100644 --- a/src/flatten_ok.rs +++ b/src/flatten_ok.rs @@ -73,11 +73,8 @@ where } fn size_hint(&self) -> (usize, Option) { - let inner_hint = |inner: &Option| { - inner - .as_ref() - .map_or((0, Some(0)), Iterator::size_hint) - }; + let inner_hint = + |inner: &Option| inner.as_ref().map_or((0, Some(0)), Iterator::size_hint); let inner_front = inner_hint(&self.inner_front); let inner_back = inner_hint(&self.inner_back); // The outer iterator `Ok` case could be (0, None) as we don't know its size_hint yet. From ab4bd085d78bd403a908119286e2deecaa67cf9a Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Wed, 10 Jan 2024 13:32:19 +0100 Subject: [PATCH 11/11] fix clippy::use_self --- benches/extra/zipslices.rs | 2 +- examples/iris.rs | 4 +- src/adaptors/mod.rs | 4 +- src/adaptors/multi_product.rs | 6 +- src/duplicates_impl.rs | 6 +- src/either_or_both.rs | 144 +++++++++++++++++----------------- src/groupbylazy.rs | 2 +- src/kmerge_impl.rs | 4 +- src/lazy_buffer.rs | 4 +- src/lib.rs | 6 +- src/minmax.rs | 6 +- src/tuple_impl.rs | 2 +- src/zip_longest.rs | 2 +- tests/quick.rs | 26 +++--- tests/test_core.rs | 2 +- tests/test_std.rs | 14 ++-- 16 files changed, 117 insertions(+), 117 deletions(-) diff --git a/benches/extra/zipslices.rs b/benches/extra/zipslices.rs index a7120e168..2ec7bc0c0 100644 --- a/benches/extra/zipslices.rs +++ b/benches/extra/zipslices.rs @@ -58,7 +58,7 @@ where #[inline(always)] pub fn from_slices(a: T, b: U) -> Self { let minl = cmp::min(a.len(), b.len()); - ZipSlices { + Self { t: a, u: b, len: minl, diff --git a/examples/iris.rs b/examples/iris.rs index 798fdb08e..5f5525154 100644 --- a/examples/iris.rs +++ b/examples/iris.rs @@ -25,7 +25,7 @@ enum ParseError { impl From for ParseError { fn from(err: ParseFloatError) -> Self { - ParseError::Numeric(err) + Self::Numeric(err) } } @@ -34,7 +34,7 @@ impl FromStr for Iris { type Err = ParseError; fn from_str(s: &str) -> Result { - let mut iris = Iris { + let mut iris = Self { name: "".into(), data: [0.; 4], }; diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index a5b182d78..08d91e397 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -218,7 +218,7 @@ where /// Split the `PutBack` into its parts. #[inline] pub fn into_parts(self) -> (Option, I) { - let PutBack { top, iter } = self; + let Self { top, iter } = self; (top, iter) } @@ -689,7 +689,7 @@ pub struct Tuple1Combination { impl From for Tuple1Combination { fn from(iter: I) -> Self { - Tuple1Combination { iter } + Self { iter } } } diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index 3782f5990..a9d39562b 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -93,7 +93,7 @@ where if last.in_progress() { true - } else if MultiProduct::iterate_last(rest, state) { + } else if Self::iterate_last(rest, state) { last.reset(); last.iterate(); // If iterator is None twice consecutively, then iterator is @@ -133,7 +133,7 @@ where I::Item: Clone, { fn new(iter: I) -> Self { - MultiProductIter { + Self { cur: None, iter: iter.clone(), iter_orig: iter, @@ -165,7 +165,7 @@ where type Item = Vec; fn next(&mut self) -> Option { - if MultiProduct::iterate_last(&mut self.0, MultiProductIterState::StartOfIter) { + if Self::iterate_last(&mut self.0, MultiProductIterState::StartOfIter) { Some(self.curr_iterator()) } else { None diff --git a/src/duplicates_impl.rs b/src/duplicates_impl.rs index 71ed21841..a0db15432 100644 --- a/src/duplicates_impl.rs +++ b/src/duplicates_impl.rs @@ -22,7 +22,7 @@ mod private { impl DuplicatesBy { pub(crate) fn new(iter: I, key_method: F) -> Self { - DuplicatesBy { + Self { iter, meta: Meta { used: HashMap::new(), @@ -77,7 +77,7 @@ mod private { type Item = I::Item; fn next(&mut self) -> Option { - let DuplicatesBy { iter, meta } = self; + let Self { iter, meta } = self; iter.find_map(|v| meta.filter(v)) } @@ -109,7 +109,7 @@ mod private { F: KeyMethod, { fn next_back(&mut self) -> Option { - let DuplicatesBy { iter, meta } = self; + let Self { iter, meta } = self; iter.rev().find_map(|v| meta.filter(v)) } } diff --git a/src/either_or_both.rs b/src/either_or_both.rs index b504d69f7..26fc1192d 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -27,13 +27,13 @@ impl EitherOrBoth { /// If `Left`, return true. Otherwise, return false. /// Exclusive version of [`has_left`](EitherOrBoth::has_left). pub fn is_left(&self) -> bool { - matches!(self, EitherOrBoth::Left(_)) + matches!(self, Self::Left(_)) } /// If `Right`, return true. Otherwise, return false. /// Exclusive version of [`has_right`](EitherOrBoth::has_right). pub fn is_right(&self) -> bool { - matches!(self, EitherOrBoth::Right(_)) + matches!(self, Self::Right(_)) } /// If `Both`, return true. Otherwise, return false. @@ -44,16 +44,16 @@ impl EitherOrBoth { /// If `Left`, or `Both`, return `Some` with the left value. Otherwise, return `None`. pub fn left(self) -> Option { match self { - EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => Some(left), - EitherOrBoth::Right(_) => None, + Self::Left(left) | Self::Both(left, _) => Some(left), + Self::Right(_) => None, } } /// If `Right`, or `Both`, return `Some` with the right value. Otherwise, return `None`. pub fn right(self) -> Option { match self { - EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => Some(right), - EitherOrBoth::Left(_) => None, + Self::Right(right) | Self::Both(_, right) => Some(right), + Self::Left(_) => None, } } @@ -85,7 +85,7 @@ impl EitherOrBoth { /// ``` pub fn just_left(self) -> Option { match self { - EitherOrBoth::Left(left) => Some(left), + Self::Left(left) => Some(left), _ => None, } } @@ -110,7 +110,7 @@ impl EitherOrBoth { /// ``` pub fn just_right(self) -> Option { match self { - EitherOrBoth::Right(right) => Some(right), + Self::Right(right) => Some(right), _ => None, } } @@ -118,7 +118,7 @@ impl EitherOrBoth { /// If `Both`, return `Some` containing the left and right values. Otherwise, return `None`. pub fn both(self) -> Option<(A, B)> { match self { - EitherOrBoth::Both(a, b) => Some((a, b)), + Self::Both(a, b) => Some((a, b)), _ => None, } } @@ -129,8 +129,8 @@ impl EitherOrBoth { B: Into, { match self { - EitherOrBoth::Left(a) | EitherOrBoth::Both(a, _) => a, - EitherOrBoth::Right(b) => b.into(), + Self::Left(a) | Self::Both(a, _) => a, + Self::Right(b) => b.into(), } } @@ -140,26 +140,26 @@ impl EitherOrBoth { A: Into, { match self { - EitherOrBoth::Right(b) | EitherOrBoth::Both(_, b) => b, - EitherOrBoth::Left(a) => a.into(), + Self::Right(b) | Self::Both(_, b) => b, + Self::Left(a) => a.into(), } } /// Converts from `&EitherOrBoth` to `EitherOrBoth<&A, &B>`. pub fn as_ref(&self) -> EitherOrBoth<&A, &B> { match *self { - EitherOrBoth::Left(ref left) => EitherOrBoth::Left(left), - EitherOrBoth::Right(ref right) => EitherOrBoth::Right(right), - EitherOrBoth::Both(ref left, ref right) => EitherOrBoth::Both(left, right), + Self::Left(ref left) => EitherOrBoth::Left(left), + Self::Right(ref right) => EitherOrBoth::Right(right), + Self::Both(ref left, ref right) => EitherOrBoth::Both(left, right), } } /// Converts from `&mut EitherOrBoth` to `EitherOrBoth<&mut A, &mut B>`. pub fn as_mut(&mut self) -> EitherOrBoth<&mut A, &mut B> { match *self { - EitherOrBoth::Left(ref mut left) => EitherOrBoth::Left(left), - EitherOrBoth::Right(ref mut right) => EitherOrBoth::Right(right), - EitherOrBoth::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), + Self::Left(ref mut left) => EitherOrBoth::Left(left), + Self::Right(ref mut right) => EitherOrBoth::Right(right), + Self::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), } } @@ -170,9 +170,9 @@ impl EitherOrBoth { B: Deref, { match *self { - EitherOrBoth::Left(ref left) => EitherOrBoth::Left(left), - EitherOrBoth::Right(ref right) => EitherOrBoth::Right(right), - EitherOrBoth::Both(ref left, ref right) => EitherOrBoth::Both(left, right), + Self::Left(ref left) => EitherOrBoth::Left(left), + Self::Right(ref right) => EitherOrBoth::Right(right), + Self::Both(ref left, ref right) => EitherOrBoth::Both(left, right), } } @@ -183,18 +183,18 @@ impl EitherOrBoth { B: DerefMut, { match *self { - EitherOrBoth::Left(ref mut left) => EitherOrBoth::Left(left), - EitherOrBoth::Right(ref mut right) => EitherOrBoth::Right(right), - EitherOrBoth::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), + Self::Left(ref mut left) => EitherOrBoth::Left(left), + Self::Right(ref mut right) => EitherOrBoth::Right(right), + Self::Both(ref mut left, ref mut right) => EitherOrBoth::Both(left, right), } } /// Convert `EitherOrBoth` to `EitherOrBoth`. pub fn flip(self) -> EitherOrBoth { match self { - EitherOrBoth::Left(a) => EitherOrBoth::Right(a), - EitherOrBoth::Right(b) => EitherOrBoth::Left(b), - EitherOrBoth::Both(a, b) => EitherOrBoth::Both(b, a), + Self::Left(a) => EitherOrBoth::Right(a), + Self::Right(b) => EitherOrBoth::Left(b), + Self::Both(a, b) => EitherOrBoth::Both(b, a), } } @@ -205,9 +205,9 @@ impl EitherOrBoth { F: FnOnce(A) -> M, { match self { - EitherOrBoth::Both(a, b) => EitherOrBoth::Both(f(a), b), - EitherOrBoth::Left(a) => EitherOrBoth::Left(f(a)), - EitherOrBoth::Right(b) => EitherOrBoth::Right(b), + Self::Both(a, b) => EitherOrBoth::Both(f(a), b), + Self::Left(a) => EitherOrBoth::Left(f(a)), + Self::Right(b) => EitherOrBoth::Right(b), } } @@ -218,9 +218,9 @@ impl EitherOrBoth { F: FnOnce(B) -> M, { match self { - EitherOrBoth::Left(a) => EitherOrBoth::Left(a), - EitherOrBoth::Right(b) => EitherOrBoth::Right(f(b)), - EitherOrBoth::Both(a, b) => EitherOrBoth::Both(a, f(b)), + Self::Left(a) => EitherOrBoth::Left(a), + Self::Right(b) => EitherOrBoth::Right(f(b)), + Self::Both(a, b) => EitherOrBoth::Both(a, f(b)), } } @@ -233,9 +233,9 @@ impl EitherOrBoth { G: FnOnce(B) -> R, { match self { - EitherOrBoth::Left(a) => EitherOrBoth::Left(f(a)), - EitherOrBoth::Right(b) => EitherOrBoth::Right(g(b)), - EitherOrBoth::Both(a, b) => EitherOrBoth::Both(f(a), g(b)), + Self::Left(a) => EitherOrBoth::Left(f(a)), + Self::Right(b) => EitherOrBoth::Right(g(b)), + Self::Both(a, b) => EitherOrBoth::Both(f(a), g(b)), } } @@ -246,8 +246,8 @@ impl EitherOrBoth { F: FnOnce(A) -> EitherOrBoth, { match self { - EitherOrBoth::Left(a) | EitherOrBoth::Both(a, _) => f(a), - EitherOrBoth::Right(b) => EitherOrBoth::Right(b), + Self::Left(a) | Self::Both(a, _) => f(a), + Self::Right(b) => EitherOrBoth::Right(b), } } @@ -258,8 +258,8 @@ impl EitherOrBoth { F: FnOnce(B) -> EitherOrBoth, { match self { - EitherOrBoth::Left(a) => EitherOrBoth::Left(a), - EitherOrBoth::Right(b) | EitherOrBoth::Both(_, b) => f(b), + Self::Left(a) => EitherOrBoth::Left(a), + Self::Right(b) | Self::Both(_, b) => f(b), } } @@ -284,9 +284,9 @@ impl EitherOrBoth { /// ``` pub fn or(self, l: A, r: B) -> (A, B) { match self { - EitherOrBoth::Left(inner_l) => (inner_l, r), - EitherOrBoth::Right(inner_r) => (l, inner_r), - EitherOrBoth::Both(inner_l, inner_r) => (inner_l, inner_r), + Self::Left(inner_l) => (inner_l, r), + Self::Right(inner_r) => (l, inner_r), + Self::Both(inner_l, inner_r) => (inner_l, inner_r), } } @@ -299,9 +299,9 @@ impl EitherOrBoth { B: Default, { match self { - EitherOrBoth::Left(l) => (l, B::default()), - EitherOrBoth::Right(r) => (A::default(), r), - EitherOrBoth::Both(l, r) => (l, r), + Self::Left(l) => (l, B::default()), + Self::Right(r) => (A::default(), r), + Self::Both(l, r) => (l, r), } } @@ -321,9 +321,9 @@ impl EitherOrBoth { /// ``` pub fn or_else A, R: FnOnce() -> B>(self, l: L, r: R) -> (A, B) { match self { - EitherOrBoth::Left(inner_l) => (inner_l, r()), - EitherOrBoth::Right(inner_r) => (l(), inner_r), - EitherOrBoth::Both(inner_l, inner_r) => (inner_l, inner_r), + Self::Left(inner_l) => (inner_l, r()), + Self::Right(inner_r) => (l(), inner_r), + Self::Both(inner_l, inner_r) => (inner_l, inner_r), } } @@ -346,8 +346,8 @@ impl EitherOrBoth { F: FnOnce() -> A, { match self { - EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => left, - EitherOrBoth::Right(_) => self.insert_left(f()), + Self::Left(left) | Self::Both(left, _) => left, + Self::Right(_) => self.insert_left(f()), } } @@ -358,8 +358,8 @@ impl EitherOrBoth { F: FnOnce() -> B, { match self { - EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => right, - EitherOrBoth::Left(_) => self.insert_right(f()), + Self::Right(right) | Self::Both(_, right) => right, + Self::Left(_) => self.insert_right(f()), } } @@ -381,21 +381,21 @@ impl EitherOrBoth { /// ``` pub fn insert_left(&mut self, val: A) -> &mut A { match self { - EitherOrBoth::Left(left) | EitherOrBoth::Both(left, _) => { + Self::Left(left) | Self::Both(left, _) => { *left = val; left } - EitherOrBoth::Right(right) => { + Self::Right(right) => { // This is like a map in place operation. We move out of the reference, // change the value, and then move back into the reference. unsafe { // SAFETY: We know this pointer is valid for reading since we got it from a reference. let right = std::ptr::read(right as *mut _); // SAFETY: Again, we know the pointer is valid since we got it from a reference. - std::ptr::write(self as *mut _, EitherOrBoth::Both(val, right)); + std::ptr::write(self as *mut _, Self::Both(val, right)); } - if let EitherOrBoth::Both(left, _) = self { + if let Self::Both(left, _) = self { left } else { // SAFETY: The above pattern will always match, since we just @@ -423,20 +423,20 @@ impl EitherOrBoth { /// ``` pub fn insert_right(&mut self, val: B) -> &mut B { match self { - EitherOrBoth::Right(right) | EitherOrBoth::Both(_, right) => { + Self::Right(right) | Self::Both(_, right) => { *right = val; right } - EitherOrBoth::Left(left) => { + Self::Left(left) => { // This is like a map in place operation. We move out of the reference, // change the value, and then move back into the reference. unsafe { // SAFETY: We know this pointer is valid for reading since we got it from a reference. let left = std::ptr::read(left as *mut _); // SAFETY: Again, we know the pointer is valid since we got it from a reference. - std::ptr::write(self as *mut _, EitherOrBoth::Both(left, val)); + std::ptr::write(self as *mut _, Self::Both(left, val)); } - if let EitherOrBoth::Both(_, right) = self { + if let Self::Both(_, right) = self { right } else { // SAFETY: The above pattern will always match, since we just @@ -450,8 +450,8 @@ impl EitherOrBoth { /// Set `self` to `Both(..)`, containing the specified left and right values, /// and returns a mutable reference to those values. pub fn insert_both(&mut self, left: A, right: B) -> (&mut A, &mut B) { - *self = EitherOrBoth::Both(left, right); - if let EitherOrBoth::Both(left, right) = self { + *self = Self::Both(left, right); + if let Self::Both(left, right) = self { (left, right) } else { // SAFETY: The above pattern will always match, since we just @@ -485,9 +485,9 @@ impl EitherOrBoth { F: FnOnce(T, T) -> T, { match self { - EitherOrBoth::Left(a) => a, - EitherOrBoth::Right(b) => b, - EitherOrBoth::Both(a, b) => f(a, b), + Self::Left(a) => a, + Self::Right(b) => b, + Self::Both(a, b) => f(a, b), } } } @@ -496,9 +496,9 @@ impl EitherOrBoth { impl Into>> for EitherOrBoth { fn into(self) -> Option> { match self { - EitherOrBoth::Left(l) => Some(Either::Left(l)), - EitherOrBoth::Right(r) => Some(Either::Right(r)), - EitherOrBoth::Both(..) => None, + Self::Left(l) => Some(Either::Left(l)), + Self::Right(r) => Some(Either::Right(r)), + Self::Both(..) => None, } } } @@ -506,8 +506,8 @@ impl Into>> for EitherOrBoth { impl From> for EitherOrBoth { fn from(either: Either) -> Self { match either { - Either::Left(l) => EitherOrBoth::Left(l), - Either::Right(l) => EitherOrBoth::Right(l), + Either::Left(l) => Self::Left(l), + Either::Right(l) => Self::Right(l), } } } diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index 6279899d0..ab553b414 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -29,7 +29,7 @@ struct ChunkIndex { impl ChunkIndex { #[inline(always)] fn new(size: usize) -> Self { - ChunkIndex { + Self { size, index: 0, key: 0, diff --git a/src/kmerge_impl.rs b/src/kmerge_impl.rs index af1462679..7f1e50e94 100644 --- a/src/kmerge_impl.rs +++ b/src/kmerge_impl.rs @@ -27,9 +27,9 @@ where I: Iterator, { /// Constructs a `HeadTail` from an `Iterator`. Returns `None` if the `Iterator` is empty. - fn new(mut it: I) -> Option> { + fn new(mut it: I) -> Option { let head = it.next(); - head.map(|h| HeadTail { head: h, tail: it }) + head.map(|h| Self { head: h, tail: it }) } /// Get the next element and update `head`, returning the old head in `Some`. diff --git a/src/lazy_buffer.rs b/src/lazy_buffer.rs index 38c7d405b..5cb039a63 100644 --- a/src/lazy_buffer.rs +++ b/src/lazy_buffer.rs @@ -14,8 +14,8 @@ impl LazyBuffer where I: Iterator, { - pub fn new(it: I) -> LazyBuffer { - LazyBuffer { + pub fn new(it: I) -> Self { + Self { it: it.fuse(), buffer: Vec::new(), } diff --git a/src/lib.rs b/src/lib.rs index 5845b9496..938f687b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4076,15 +4076,15 @@ impl FoldWhile { /// Return the value in the continue or done. pub fn into_inner(self) -> T { match self { - FoldWhile::Continue(x) | FoldWhile::Done(x) => x, + Self::Continue(x) | Self::Done(x) => x, } } /// Return true if `self` is `Done`, false if it is `Continue`. pub fn is_done(&self) -> bool { match *self { - FoldWhile::Continue(_) => false, - FoldWhile::Done(_) => true, + Self::Continue(_) => false, + Self::Done(_) => true, } } } diff --git a/src/minmax.rs b/src/minmax.rs index e522bcb4f..802b1e2f0 100644 --- a/src/minmax.rs +++ b/src/minmax.rs @@ -37,9 +37,9 @@ impl MinMaxResult { /// ``` pub fn into_option(self) -> Option<(T, T)> { match self { - MinMaxResult::NoElements => None, - MinMaxResult::OneElement(x) => Some((x.clone(), x)), - MinMaxResult::MinMax(x, y) => Some((x, y)), + Self::NoElements => None, + Self::OneElement(x) => Some((x.clone(), x)), + Self::MinMax(x, y) => Some((x, y)), } } } diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index f65941965..b1cfc3d5f 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -34,7 +34,7 @@ where T: HomogeneousTuple, { fn new(buf: T::Buffer) -> Self { - TupleBuffer { cur: 0, buf } + Self { cur: 0, buf } } } diff --git a/src/zip_longest.rs b/src/zip_longest.rs index 27d9f3ab6..e53733542 100644 --- a/src/zip_longest.rs +++ b/src/zip_longest.rs @@ -59,7 +59,7 @@ where Self: Sized, F: FnMut(B, Self::Item) -> B, { - let ZipLongest { a, mut b } = self; + let Self { a, mut b } = self; init = a.fold(init, |init, a| match b.next() { Some(b) => f(init, EitherOrBoth::Both(a, b)), None => f(init, EitherOrBoth::Left(a)), diff --git a/tests/quick.rs b/tests/quick.rs index 0ca32f49a..dffcd22f6 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -38,7 +38,7 @@ impl HintKind for Exact { impl qc::Arbitrary for Exact { fn arbitrary(_: &mut G) -> Self { - Exact {} + Self {} } } @@ -69,7 +69,7 @@ impl qc::Arbitrary for Inexact { // Compensate for quickcheck using extreme values too rarely let ue_choices = &[0, ue_value, usize::max_value()]; let oe_choices = &[0, oe_value, usize::max_value()]; - Inexact { + Self { underestimate: *ue_choices.choose(g).unwrap(), overestimate: *oe_choices.choose(g).unwrap(), } @@ -79,7 +79,7 @@ impl qc::Arbitrary for Inexact { let underestimate_value = self.underestimate; let overestimate_value = self.overestimate; Box::new(underestimate_value.shrink().flat_map(move |ue_value| { - overestimate_value.shrink().map(move |oe_value| Inexact { + overestimate_value.shrink().map(move |oe_value| Self { underestimate: ue_value, overestimate: oe_value, }) @@ -108,7 +108,7 @@ where HK: HintKind, { fn new(it: Range, hint_kind: HK) -> Self { - Iter { + Self { iterator: it, fuse_flag: 0, hint_kind, @@ -166,16 +166,16 @@ where HK: HintKind, { fn arbitrary(g: &mut G) -> Self { - Iter::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g)) + Self::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g)) } - fn shrink(&self) -> Box>> { + fn shrink(&self) -> Box> { let r = self.iterator.clone(); let hint_kind = self.hint_kind; Box::new(r.start.shrink().flat_map(move |a| { r.end .shrink() - .map(move |b| Iter::new(a.clone()..b, hint_kind)) + .map(move |b| Self::new(a.clone()..b, hint_kind)) })) } } @@ -231,7 +231,7 @@ where let iter_count = g.gen_range(0, MAX_ITER_COUNT + 1); let hint_kind = qc::Arbitrary::arbitrary(g); - ShiftRange { + Self { range_start, range_end, start_step, @@ -1307,14 +1307,14 @@ quickcheck! { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); -impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { +impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } } @@ -1322,10 +1322,10 @@ impl Ord for Val { impl qc::Arbitrary for Val { fn arbitrary(g: &mut G) -> Self { let (x, y) = <(u32, u32)>::arbitrary(g); - Val(x, y) + Self(x, y) } fn shrink(&self) -> Box> { - Box::new((self.0, self.1).shrink().map(|(x, y)| Val(x, y))) + Box::new((self.0, self.1).shrink().map(|(x, y)| Self(x, y))) } } diff --git a/tests/test_core.rs b/tests/test_core.rs index 86625c8cf..263e1a7d3 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -237,7 +237,7 @@ fn count_clones() { fn clone(&self) -> Self { let n = self.n.get(); self.n.set(n + 1); - Foo { + Self { n: Cell::new(n + 1), } } diff --git a/tests/test_std.rs b/tests/test_std.rs index 24429e919..063eca049 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -540,7 +540,7 @@ where impl qc::Arbitrary for RandIter { fn arbitrary(g: &mut G) -> Self { - RandIter { + Self { idx: 0, len: g.size(), rng: R::seed_from_u64(g.next_u64()), @@ -1263,14 +1263,14 @@ fn extrema_set() { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); - impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { + impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } } @@ -1312,14 +1312,14 @@ fn minmax() { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); - impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { + impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } }