Skip to content

Commit 03f0731

Browse files
committed
Simpler checks for specialized fold
Instead of XORing everything, remember all the intermediate parameters and simply push elements into a Vec, relaxing the type constraints.
1 parent b00d7c6 commit 03f0731

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

tests/specializations.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use itertools::{EitherOrBoth, Itertools};
1+
use itertools::Itertools;
22
use std::fmt::Debug;
3-
use std::ops::BitXor;
43
use quickcheck::quickcheck;
54

65
struct Unspecialized<I>(I);
@@ -63,21 +62,19 @@ fn check_specialized_count_last_nth_sizeh<'a, IterItem, Iter>(
6362
}
6463
}
6564

66-
fn check_specialized_fold_xor<'a, IterItem, Iter>(it: &Iter)
65+
fn check_specialized_fold<IterItem, Iter>(it: &Iter)
6766
where
68-
IterItem: 'a
69-
+ BitXor
70-
+ Eq
71-
+ Debug
72-
+ BitXor<<IterItem as BitXor>::Output, Output = <IterItem as BitXor>::Output>
73-
+ Clone,
74-
<IterItem as BitXor>::Output:
75-
BitXor<Output = <IterItem as BitXor>::Output> + Eq + Debug + Clone,
76-
Iter: Iterator<Item = IterItem> + Clone + 'a,
67+
IterItem: Eq + Debug + Clone,
68+
Iter: Iterator<Item = IterItem> + Clone,
7769
{
78-
check_specialized(it, |mut i| {
79-
let first = i.next().map(|f| f.clone() ^ (f.clone() ^ f));
80-
i.fold(first, |acc, v: IterItem| acc.map(move |a| v ^ a))
70+
check_specialized(it, |i| {
71+
let mut parameters_from_fold = vec![];
72+
let fold_result = i.fold(vec![], |mut acc, v: IterItem| {
73+
parameters_from_fold.push((acc.clone(), v.clone()));
74+
acc.push(v);
75+
acc
76+
});
77+
(parameters_from_fold, fold_result)
8178
});
8279
}
8380

@@ -86,13 +83,13 @@ fn put_back_test(test_vec: Vec<i32>, known_expected_size: Option<usize>) {
8683
// Lexical lifetimes support
8784
let pb = itertools::put_back(test_vec.iter());
8885
check_specialized_count_last_nth_sizeh(&pb, known_expected_size);
89-
check_specialized_fold_xor(&pb);
86+
check_specialized_fold(&pb);
9087
}
9188

9289
let mut pb = itertools::put_back(test_vec.into_iter());
9390
pb.put_back(1);
9491
check_specialized_count_last_nth_sizeh(&pb, known_expected_size.map(|x| x + 1));
95-
check_specialized_fold_xor(&pb)
92+
check_specialized_fold(&pb)
9693
}
9794

9895
#[test]
@@ -111,28 +108,12 @@ fn merge_join_by_test(i1: Vec<usize>, i2: Vec<usize>, known_expected_size: Optio
111108
let i2 = i2.into_iter();
112109
let mjb = i1.clone().merge_join_by(i2.clone(), std::cmp::Ord::cmp);
113110
check_specialized_count_last_nth_sizeh(&mjb, known_expected_size);
114-
// Rust 1.24 compatibility:
115-
fn eob_left_z(eob: EitherOrBoth<usize, usize>) -> usize {
116-
eob.left().unwrap_or(0)
117-
}
118-
fn eob_right_z(eob: EitherOrBoth<usize, usize>) -> usize {
119-
eob.right().unwrap_or(0)
120-
}
121-
fn eob_both_z(eob: EitherOrBoth<usize, usize>) -> usize {
122-
let (a, b) = eob.both().unwrap_or((0, 0));
123-
assert_eq!(a, b);
124-
a
125-
}
126-
check_specialized_fold_xor(&mjb.clone().map(eob_left_z));
127-
check_specialized_fold_xor(&mjb.clone().map(eob_right_z));
128-
check_specialized_fold_xor(&mjb.clone().map(eob_both_z));
111+
check_specialized_fold(&mjb);
129112

130113
// And the other way around
131114
let mjb = i2.merge_join_by(i1, std::cmp::Ord::cmp);
132115
check_specialized_count_last_nth_sizeh(&mjb, known_expected_size);
133-
check_specialized_fold_xor(&mjb.clone().map(eob_left_z));
134-
check_specialized_fold_xor(&mjb.clone().map(eob_right_z));
135-
check_specialized_fold_xor(&mjb.clone().map(eob_both_z));
116+
check_specialized_fold(&mjb);
136117
}
137118

138119
#[test]

0 commit comments

Comments
 (0)