From 314bf10b1f65fc63b34066dfdbf8b8cb839efd0d Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Sun, 27 Aug 2023 20:36:05 +0700 Subject: [PATCH] build: resolve warnings Co-authored-by: warren2k <846021+warren2k@users.noreply.github.com> --- benches/fold_specialization.rs | 12 +++++++---- benches/tree_fold1.rs | 12 +++++++---- tests/quick.rs | 18 ++++++++++------ tests/specializations.rs | 7 ++++++- tests/test_core.rs | 6 +++++- tests/test_std.rs | 9 +++++--- wrappers.rs | 38 ++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 wrappers.rs diff --git a/benches/fold_specialization.rs b/benches/fold_specialization.rs index 5de4671e9..6000880f8 100644 --- a/benches/fold_specialization.rs +++ b/benches/fold_specialization.rs @@ -1,5 +1,9 @@ +#[path = "../wrappers.rs"] +mod wrappers; + +use wrappers::Ext; + use criterion::{criterion_group, criterion_main, Criterion}; -use itertools::Itertools; struct Unspecialized(I); @@ -32,7 +36,7 @@ mod specialization { c.bench_function("external", move |b| { b.iter(|| { let mut sum = 0; - for &x in arr.iter().intersperse(&0) { + for &x in arr.iter().intersperse_wrap(&0) { sum += x; } sum @@ -46,7 +50,7 @@ mod specialization { c.bench_function("internal specialized", move |b| { b.iter(|| { - arr.iter().intersperse(&0).fold(0, |acc, x| acc + x) + arr.iter().intersperse_wrap(&0).fold(0, |acc, x| acc + x) }) }); } @@ -57,7 +61,7 @@ mod specialization { c.bench_function("internal unspecialized", move |b| { b.iter(|| { - Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x) + Unspecialized(arr.iter().intersperse_wrap(&0)).fold(0, |acc, x| acc + x) }) }); } diff --git a/benches/tree_fold1.rs b/benches/tree_fold1.rs index f12995db8..81ab7c13b 100644 --- a/benches/tree_fold1.rs +++ b/benches/tree_fold1.rs @@ -1,5 +1,9 @@ +#[path = "../wrappers.rs"] +mod wrappers; + use criterion::{criterion_group, criterion_main, Criterion}; use itertools::{Itertools, cloned}; +use wrappers::Ext; trait IterEx : Iterator { // Another efficient implementation against which to compare, @@ -18,7 +22,7 @@ trait IterEx : Iterator { } stack.push(x); }); - stack.into_iter().fold1(f) + stack.into_iter().fold1_wrap(f) } } impl IterEx for T {} @@ -79,7 +83,7 @@ macro_rules! def_benchs { def_benchs!{ 10_000, - fold1, + fold1_wrap, fold1_10k, } @@ -97,7 +101,7 @@ def_benchs!{ def_benchs!{ 100, - fold1, + fold1_wrap, fold1_100, } @@ -115,7 +119,7 @@ def_benchs!{ def_benchs!{ 8, - fold1, + fold1_wrap, fold1_08, } diff --git a/tests/quick.rs b/tests/quick.rs index c19af6c1e..d9382738a 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -3,6 +3,9 @@ //! //! In particular we test the tedious size_hint and exact size correctness. +#[path = "../wrappers.rs"] +mod wrappers; + use quickcheck as qc; use std::default::Default; use std::num::Wrapping; @@ -24,14 +27,17 @@ use itertools::free::{ put_back, put_back_n, rciter, - zip, zip_eq, }; +use wrappers::free::zip; + use rand::Rng; use rand::seq::SliceRandom; use quickcheck::TestResult; +use crate::wrappers::Ext; + /// Trait for size hint modifier types trait HintKind: Copy + Send + qc::Arbitrary { fn loosen_bounds(&self, org_hint: (usize, Option)) -> (usize, Option); @@ -635,12 +641,12 @@ quickcheck! { exact_size_for_this(a.iter().interleave_shortest(&b)) } fn size_intersperse(a: Iter, x: i16) -> bool { - correct_size_hint(a.intersperse(x)) + correct_size_hint(a.intersperse_wrap(x)) } fn equal_intersperse(a: Vec, x: i32) -> bool { let mut inter = false; let mut i = 0; - for elt in a.iter().cloned().intersperse(x) { + for elt in a.iter().cloned().intersperse_wrap(x) { if inter { if elt != x { return false } } else { @@ -1243,8 +1249,8 @@ quickcheck! { return TestResult::discard(); } - let min = cloned(&a).fold1(f64::min); - let max = cloned(&a).fold1(f64::max); + let min = cloned(&a).fold1_wrap(f64::min); + let max = cloned(&a).fold1_wrap(f64::max); let minmax = cloned(&a).minmax(); let expected = match a.len() { @@ -1405,7 +1411,7 @@ quickcheck! { .map(|i| (i % modulo, i)) .into_group_map() .into_iter() - .map(|(key, vals)| (key, vals.into_iter().fold1(|acc, val| acc + val).unwrap())) + .map(|(key, vals)| (key, vals.into_iter().fold1_wrap(|acc, val| acc + val).unwrap())) .collect::>(); assert_eq!(lookup, group_map_lookup); diff --git a/tests/specializations.rs b/tests/specializations.rs index 057e11c9f..b67707665 100644 --- a/tests/specializations.rs +++ b/tests/specializations.rs @@ -1,7 +1,12 @@ +#[path = "../wrappers.rs"] +mod wrappers; + use itertools::Itertools; use std::fmt::Debug; use quickcheck::quickcheck; +use crate::wrappers::Ext; + struct Unspecialized(I); impl Iterator for Unspecialized where @@ -74,7 +79,7 @@ fn test_specializations( quickcheck! { fn intersperse(v: Vec) -> () { - test_specializations(&v.into_iter().intersperse(0)); + test_specializations(&v.into_iter().intersperse_wrap(0)); } } diff --git a/tests/test_core.rs b/tests/test_core.rs index df94eb665..addd42400 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -5,6 +5,9 @@ //! except according to those terms. #![no_std] +#[path = "../wrappers.rs"] +mod wrappers; + use core::iter; use itertools as it; use crate::it::Itertools; @@ -16,6 +19,7 @@ use crate::it::free::put_back; use crate::it::iproduct; use crate::it::izip; use crate::it::chain; +use crate::wrappers::Ext; #[test] fn product2() { @@ -278,7 +282,7 @@ fn part() { #[test] fn tree_fold1() { for i in 0..100 { - assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y)); + assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1_wrap(|x, y| x + y)); } } diff --git a/tests/test_std.rs b/tests/test_std.rs index 8ea992183..42320f5ee 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -1,9 +1,12 @@ +#[path = "../wrappers.rs"] +mod wrappers; + use quickcheck as qc; use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng}; use rand::{seq::SliceRandom, thread_rng}; use std::{cmp::min, fmt::Debug, marker::PhantomData}; use itertools as it; -use crate::it::Itertools; +use crate::{it::Itertools, wrappers::Ext}; use crate::it::ExactlyOneError; use crate::it::multizip; use crate::it::multipeek; @@ -121,12 +124,12 @@ fn unique() { #[test] fn intersperse() { let xs = ["a", "", "b", "c"]; - let v: Vec<&str> = xs.iter().cloned().intersperse(", ").collect(); + let v: Vec<&str> = xs.iter().cloned().intersperse_wrap(", ").collect(); let text: String = v.concat(); assert_eq!(text, "a, , b, c".to_string()); let ys = [0, 1, 2, 3]; - let mut it = ys[..0].iter().copied().intersperse(1); + let mut it = ys[..0].iter().copied().intersperse_wrap(1); assert!(it.next() == None); } diff --git a/wrappers.rs b/wrappers.rs new file mode 100644 index 000000000..b94a5f215 --- /dev/null +++ b/wrappers.rs @@ -0,0 +1,38 @@ +//! This module helps suppress two kinds of warnings: `deprecated` and `unstable_name_collisions`. +//! New items are created that are noop-wrappers of the original items. +//! The items' original paths are preserved. + +use itertools::Itertools; + +pub mod free { + // it seems the compiler is not able to discern that this is used + #[allow(dead_code)] + pub fn zip(i: I, j: J) -> core::iter::Zip + where I: IntoIterator, + J: IntoIterator + { + #[allow(deprecated)] + itertools::free::zip(i, j) + } +} + +pub trait Ext: Itertools { + fn intersperse_wrap(self, element: Self::Item) -> itertools::Intersperse + where + Self: Sized, + Self::Item: Clone, + { + ::intersperse(self, element) + } + + fn fold1_wrap(self, f: F) -> Option + where F: FnMut(Self::Item, Self::Item) -> Self::Item, + Self: Sized, + { + #[allow(deprecated)] + ::fold1(self, f) + } +} + +impl Ext for T {} +