From e75a656b48c10da6d58d298516f05b5518ec0b76 Mon Sep 17 00:00:00 2001 From: Joel Montes de Oca <6587811+JoelMon@users.noreply.github.com> Date: Fri, 8 Jul 2022 11:38:19 -0400 Subject: [PATCH 1/2] Added body to free::zip Added body to the code example in the docs for free::zip. The code body added uses two types to make it obvious how `zip` functions. --- src/free.rs | 106 +++++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 46 deletions(-) diff --git a/src/free.rs b/src/free.rs index 3886f5f1f..8889b5f62 100644 --- a/src/free.rs +++ b/src/free.rs @@ -10,30 +10,24 @@ use std::iter::{self, Zip}; type VecIntoIter = alloc::vec::IntoIter; #[cfg(feature = "use_alloc")] -use alloc::{ - string::String, -}; +use alloc::string::String; -use crate::Itertools; use crate::intersperse::{Intersperse, IntersperseWith}; +use crate::Itertools; -pub use crate::adaptors::{ - interleave, - merge, - put_back, -}; +pub use crate::adaptors::{interleave, merge, put_back}; #[cfg(feature = "use_alloc")] -pub use crate::put_back_n_impl::put_back_n; +pub use crate::kmerge_impl::kmerge; +pub use crate::merge_join::merge_join_by; #[cfg(feature = "use_alloc")] pub use crate::multipeek_impl::multipeek; #[cfg(feature = "use_alloc")] pub use crate::peek_nth::peek_nth; #[cfg(feature = "use_alloc")] -pub use crate::kmerge_impl::kmerge; -pub use crate::zip_eq_impl::zip_eq; -pub use crate::merge_join::merge_join_by; +pub use crate::put_back_n_impl::put_back_n; #[cfg(feature = "use_alloc")] pub use crate::rciter_impl::rciter; +pub use crate::zip_eq_impl::zip_eq; /// Iterate `iterable` with a particular value inserted between each element. /// @@ -45,8 +39,9 @@ pub use crate::rciter_impl::rciter; /// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]); /// ``` pub fn intersperse(iterable: I, element: I::Item) -> Intersperse - where I: IntoIterator, - ::Item: Clone +where + I: IntoIterator, + ::Item: Clone, { Itertools::intersperse(iterable.into_iter(), element) } @@ -64,8 +59,9 @@ pub fn intersperse(iterable: I, element: I::Item) -> Intersperse /// assert_eq!(i, 8); /// ``` pub fn intersperse_with(iterable: I, element: F) -> IntersperseWith - where I: IntoIterator, - F: FnMut() -> I::Item +where + I: IntoIterator, + F: FnMut() -> I::Item, { Itertools::intersperse_with(iterable.into_iter(), element) } @@ -82,7 +78,8 @@ pub fn intersperse_with(iterable: I, element: F) -> IntersperseWith(iterable: I) -> iter::Enumerate - where I: IntoIterator +where + I: IntoIterator, { iterable.into_iter().enumerate() } @@ -99,8 +96,9 @@ pub fn enumerate(iterable: I) -> iter::Enumerate /// } /// ``` pub fn rev(iterable: I) -> iter::Rev - where I: IntoIterator, - I::IntoIter: DoubleEndedIterator +where + I: IntoIterator, + I::IntoIter: DoubleEndedIterator, { iterable.into_iter().rev() } @@ -112,14 +110,20 @@ pub fn rev(iterable: I) -> iter::Rev /// ``` /// use itertools::zip; /// -/// let data = [1, 2, 3, 4, 5]; -/// for (a, b) in zip(&data, &data[1..]) { -/// /* loop body */ +/// let data_1 = [1, 2, 3, 4, 5]; +/// let data_2 = ['a', 'b', 'c']; +/// let mut result: Vec<(i32, char)> = Vec::new(); +/// +/// for (a, b) in zip(&data_1, &data_2) { +/// result.push((*a, *b)); /// } +/// assert_eq!(result, vec![(1, 'a'),(2, 'b'),(3, 'c')]); /// ``` +/// pub fn zip(i: I, j: J) -> Zip - where I: IntoIterator, - J: IntoIterator +where + I: IntoIterator, + J: IntoIterator, { i.into_iter().zip(j) } @@ -135,9 +139,13 @@ pub fn zip(i: I, j: J) -> Zip /// /* loop body */ /// } /// ``` -pub fn chain(i: I, j: J) -> iter::Chain<::IntoIter, ::IntoIter> - where I: IntoIterator, - J: IntoIterator +pub fn chain( + i: I, + j: J, +) -> iter::Chain<::IntoIter, ::IntoIter> +where + I: IntoIterator, + J: IntoIterator, { i.into_iter().chain(j) } @@ -152,8 +160,9 @@ pub fn chain(i: I, j: J) -> iter::Chain<::IntoIter, (iterable: I) -> iter::Cloned - where I: IntoIterator, - T: Clone, +where + I: IntoIterator, + T: Clone, { iterable.into_iter().cloned() } @@ -168,8 +177,9 @@ pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned /// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.); /// ``` pub fn fold(iterable: I, init: B, f: F) -> B - where I: IntoIterator, - F: FnMut(B, I::Item) -> B +where + I: IntoIterator, + F: FnMut(B, I::Item) -> B, { iterable.into_iter().fold(init, f) } @@ -184,8 +194,9 @@ pub fn fold(iterable: I, init: B, f: F) -> B /// assert!(all(&[1, 2, 3], |elt| *elt > 0)); /// ``` pub fn all(iterable: I, f: F) -> bool - where I: IntoIterator, - F: FnMut(I::Item) -> bool +where + I: IntoIterator, + F: FnMut(I::Item) -> bool, { iterable.into_iter().all(f) } @@ -200,8 +211,9 @@ pub fn all(iterable: I, f: F) -> bool /// assert!(any(&[0, -1, 2], |elt| *elt > 0)); /// ``` pub fn any(iterable: I, f: F) -> bool - where I: IntoIterator, - F: FnMut(I::Item) -> bool +where + I: IntoIterator, + F: FnMut(I::Item) -> bool, { iterable.into_iter().any(f) } @@ -216,8 +228,9 @@ pub fn any(iterable: I, f: F) -> bool /// assert_eq!(max(0..10), Some(9)); /// ``` pub fn max(iterable: I) -> Option - where I: IntoIterator, - I::Item: Ord +where + I: IntoIterator, + I::Item: Ord, { iterable.into_iter().max() } @@ -232,13 +245,13 @@ pub fn max(iterable: I) -> Option /// assert_eq!(min(0..10), Some(0)); /// ``` pub fn min(iterable: I) -> Option - where I: IntoIterator, - I::Item: Ord +where + I: IntoIterator, + I::Item: Ord, { iterable.into_iter().min() } - /// Combine all iterator elements into one String, separated by `sep`. /// /// [`IntoIterator`] enabled version of [`Itertools::join`]. @@ -250,8 +263,9 @@ pub fn min(iterable: I) -> Option /// ``` #[cfg(feature = "use_alloc")] pub fn join(iterable: I, sep: &str) -> String - where I: IntoIterator, - I::Item: Display +where + I: IntoIterator, + I::Item: Display, { iterable.into_iter().join(sep) } @@ -268,9 +282,9 @@ pub fn join(iterable: I, sep: &str) -> String /// ``` #[cfg(feature = "use_alloc")] pub fn sorted(iterable: I) -> VecIntoIter - where I: IntoIterator, - I::Item: Ord +where + I: IntoIterator, + I::Item: Ord, { iterable.into_iter().sorted() } - From 9150cab9988db6a78f6129de34d7cd4e030951a4 Mon Sep 17 00:00:00 2001 From: Joel Montes de Oca <6587811+JoelMon@users.noreply.github.com> Date: Sat, 9 Jul 2022 21:30:42 -0400 Subject: [PATCH 2/2] Reverted formatting changes --- src/free.rs | 96 ++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 53 deletions(-) diff --git a/src/free.rs b/src/free.rs index 8889b5f62..d93eb2bef 100644 --- a/src/free.rs +++ b/src/free.rs @@ -10,24 +10,30 @@ use std::iter::{self, Zip}; type VecIntoIter = alloc::vec::IntoIter; #[cfg(feature = "use_alloc")] -use alloc::string::String; +use alloc::{ + string::String, +}; -use crate::intersperse::{Intersperse, IntersperseWith}; use crate::Itertools; +use crate::intersperse::{Intersperse, IntersperseWith}; -pub use crate::adaptors::{interleave, merge, put_back}; +pub use crate::adaptors::{ + interleave, + merge, + put_back, +}; #[cfg(feature = "use_alloc")] -pub use crate::kmerge_impl::kmerge; -pub use crate::merge_join::merge_join_by; +pub use crate::put_back_n_impl::put_back_n; #[cfg(feature = "use_alloc")] pub use crate::multipeek_impl::multipeek; #[cfg(feature = "use_alloc")] pub use crate::peek_nth::peek_nth; #[cfg(feature = "use_alloc")] -pub use crate::put_back_n_impl::put_back_n; +pub use crate::kmerge_impl::kmerge; +pub use crate::zip_eq_impl::zip_eq; +pub use crate::merge_join::merge_join_by; #[cfg(feature = "use_alloc")] pub use crate::rciter_impl::rciter; -pub use crate::zip_eq_impl::zip_eq; /// Iterate `iterable` with a particular value inserted between each element. /// @@ -39,9 +45,8 @@ pub use crate::zip_eq_impl::zip_eq; /// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]); /// ``` pub fn intersperse(iterable: I, element: I::Item) -> Intersperse -where - I: IntoIterator, - ::Item: Clone, + where I: IntoIterator, + ::Item: Clone { Itertools::intersperse(iterable.into_iter(), element) } @@ -59,9 +64,8 @@ where /// assert_eq!(i, 8); /// ``` pub fn intersperse_with(iterable: I, element: F) -> IntersperseWith -where - I: IntoIterator, - F: FnMut() -> I::Item, + where I: IntoIterator, + F: FnMut() -> I::Item { Itertools::intersperse_with(iterable.into_iter(), element) } @@ -78,8 +82,7 @@ where /// } /// ``` pub fn enumerate(iterable: I) -> iter::Enumerate -where - I: IntoIterator, + where I: IntoIterator { iterable.into_iter().enumerate() } @@ -96,9 +99,8 @@ where /// } /// ``` pub fn rev(iterable: I) -> iter::Rev -where - I: IntoIterator, - I::IntoIter: DoubleEndedIterator, + where I: IntoIterator, + I::IntoIter: DoubleEndedIterator { iterable.into_iter().rev() } @@ -119,11 +121,9 @@ where /// } /// assert_eq!(result, vec![(1, 'a'),(2, 'b'),(3, 'c')]); /// ``` -/// pub fn zip(i: I, j: J) -> Zip -where - I: IntoIterator, - J: IntoIterator, + where I: IntoIterator, + J: IntoIterator { i.into_iter().zip(j) } @@ -139,13 +139,9 @@ where /// /* loop body */ /// } /// ``` -pub fn chain( - i: I, - j: J, -) -> iter::Chain<::IntoIter, ::IntoIter> -where - I: IntoIterator, - J: IntoIterator, +pub fn chain(i: I, j: J) -> iter::Chain<::IntoIter, ::IntoIter> + where I: IntoIterator, + J: IntoIterator { i.into_iter().chain(j) } @@ -160,9 +156,8 @@ where /// assert_eq!(cloned(b"abc").next(), Some(b'a')); /// ``` pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned -where - I: IntoIterator, - T: Clone, + where I: IntoIterator, + T: Clone, { iterable.into_iter().cloned() } @@ -177,9 +172,8 @@ where /// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.); /// ``` pub fn fold(iterable: I, init: B, f: F) -> B -where - I: IntoIterator, - F: FnMut(B, I::Item) -> B, + where I: IntoIterator, + F: FnMut(B, I::Item) -> B { iterable.into_iter().fold(init, f) } @@ -194,9 +188,8 @@ where /// assert!(all(&[1, 2, 3], |elt| *elt > 0)); /// ``` pub fn all(iterable: I, f: F) -> bool -where - I: IntoIterator, - F: FnMut(I::Item) -> bool, + where I: IntoIterator, + F: FnMut(I::Item) -> bool { iterable.into_iter().all(f) } @@ -211,9 +204,8 @@ where /// assert!(any(&[0, -1, 2], |elt| *elt > 0)); /// ``` pub fn any(iterable: I, f: F) -> bool -where - I: IntoIterator, - F: FnMut(I::Item) -> bool, + where I: IntoIterator, + F: FnMut(I::Item) -> bool { iterable.into_iter().any(f) } @@ -228,9 +220,8 @@ where /// assert_eq!(max(0..10), Some(9)); /// ``` pub fn max(iterable: I) -> Option -where - I: IntoIterator, - I::Item: Ord, + where I: IntoIterator, + I::Item: Ord { iterable.into_iter().max() } @@ -245,13 +236,13 @@ where /// assert_eq!(min(0..10), Some(0)); /// ``` pub fn min(iterable: I) -> Option -where - I: IntoIterator, - I::Item: Ord, + where I: IntoIterator, + I::Item: Ord { iterable.into_iter().min() } + /// Combine all iterator elements into one String, separated by `sep`. /// /// [`IntoIterator`] enabled version of [`Itertools::join`]. @@ -263,9 +254,8 @@ where /// ``` #[cfg(feature = "use_alloc")] pub fn join(iterable: I, sep: &str) -> String -where - I: IntoIterator, - I::Item: Display, + where I: IntoIterator, + I::Item: Display { iterable.into_iter().join(sep) } @@ -282,9 +272,9 @@ where /// ``` #[cfg(feature = "use_alloc")] pub fn sorted(iterable: I) -> VecIntoIter -where - I: IntoIterator, - I::Item: Ord, + where I: IntoIterator, + I::Item: Ord { iterable.into_iter().sorted() } +