From 587261f7f6bb443356af773a505dbbce2d057036 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Wed, 21 Jun 2023 10:36:50 +0200 Subject: [PATCH] Add type `Out` inside `MergePredicate` trait --- src/lib.rs | 2 +- src/merge_join.rs | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ef9e0a26..4f06c071f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1065,7 +1065,7 @@ pub trait Itertools : Iterator { /// ); /// ``` #[inline] - fn merge_join_by(self, other: J, cmp_fn: F) -> MergeJoinBy + fn merge_join_by(self, other: J, cmp_fn: F) -> MergeJoinBy where J: IntoIterator, F: FnMut(&Self::Item, &J::Item) -> T, T: merge_join::OrderingOrBool, diff --git a/src/merge_join.rs b/src/merge_join.rs index 983a0005a..9bb2fdc9f 100644 --- a/src/merge_join.rs +++ b/src/merge_join.rs @@ -2,7 +2,6 @@ use std::cmp::Ordering; use std::iter::Fuse; // use std::iter::FusedIterator; use std::fmt; -use std::marker::PhantomData; use either::Either; @@ -12,11 +11,13 @@ use crate::size_hint::{self, SizeHint}; #[cfg(doc)] use crate::Itertools; -pub trait MergePredicate { - fn merge_pred(&mut self, left: &L, right: &R) -> T; +pub trait MergePredicate { + type Out; + fn merge_pred(&mut self, left: &L, right: &R) -> Self::Out; } -impl T> MergePredicate for F { +impl T> MergePredicate for F { + type Out = T; fn merge_pred(&mut self, left: &L, right: &R) -> T { self(left, right) } @@ -25,7 +26,8 @@ impl T> MergePredicate for F { #[derive(Clone, Debug)] pub struct MergeLte; -impl MergePredicate for MergeLte { +impl MergePredicate for MergeLte { + type Out = bool; fn merge_pred(&mut self, left: &T, right: &T) -> bool { left <= right } @@ -64,19 +66,18 @@ pub fn merge(i: I, j: J) -> Merge<::IntoIter, = MergeJoinBy; +pub type MergeBy = MergeJoinBy; /// Create a `MergeBy` iterator. pub fn merge_by_new(a: I, b: J, cmp: F) -> MergeBy where I: IntoIterator, J: IntoIterator, - F: MergePredicate, + F: MergePredicate, { MergeJoinBy { left: put_back(a.into_iter().fuse()), right: put_back(b.into_iter().fuse()), cmp_fn: cmp, - _t: PhantomData, } } @@ -90,7 +91,7 @@ pub fn merge_by_new(a: I, b: J, cmp: F) -> MergeBy(left: I, right: J, cmp_fn: F) - -> MergeJoinBy + -> MergeJoinBy where I: IntoIterator, J: IntoIterator, F: FnMut(&I::Item, &J::Item) -> T, @@ -100,7 +101,6 @@ pub fn merge_join_by(left: I, right: J, cmp_fn: F) left: put_back(left.into_iter().fuse()), right: put_back(right.into_iter().fuse()), cmp_fn, - _t: PhantomData, } } @@ -108,11 +108,10 @@ pub fn merge_join_by(left: I, right: J, cmp_fn: F) /// /// See [`.merge_join_by()`](crate::Itertools::merge_join_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct MergeJoinBy { +pub struct MergeJoinBy { left: PutBack>, right: PutBack>, cmp_fn: F, - _t: PhantomData, } pub trait OrderingOrBool { @@ -195,17 +194,17 @@ impl OrderingOrBool for bool { } } -impl Clone for MergeJoinBy +impl Clone for MergeJoinBy where I: Iterator, J: Iterator, PutBack>: Clone, PutBack>: Clone, F: Clone, { - clone_fields!(left, right, cmp_fn, _t); + clone_fields!(left, right, cmp_fn); } -impl fmt::Debug for MergeJoinBy +impl fmt::Debug for MergeJoinBy where I: Iterator + fmt::Debug, I::Item: fmt::Debug, J: Iterator + fmt::Debug, @@ -214,10 +213,10 @@ impl fmt::Debug for MergeJoinBy debug_fmt_fields!(MergeJoinBy, left, right); } -impl Iterator for MergeJoinBy +impl Iterator for MergeJoinBy where I: Iterator, J: Iterator, - F: MergePredicate, + F: MergePredicate, T: OrderingOrBool, { type Item = T::MergeResult;