From 041610897cd99493ed9f810112248cdcddc89e36 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 20 Aug 2021 15:39:27 +0200 Subject: [PATCH 1/3] impl `Debug` (1) derive where possible and sensible --- src/adaptors/coalesce.rs | 4 ++-- src/adaptors/map.rs | 4 ++-- src/adaptors/mod.rs | 2 +- src/duplicates_impl.rs | 3 +++ src/kmerge_impl.rs | 2 +- src/tuple_impl.rs | 2 +- 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/adaptors/coalesce.rs b/src/adaptors/coalesce.rs index 1afbee58f..f0cddd20e 100644 --- a/src/adaptors/coalesce.rs +++ b/src/adaptors/coalesce.rs @@ -137,7 +137,7 @@ where } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct DedupEq; impl DedupPredicate for DedupEq { @@ -186,7 +186,7 @@ where pub type DedupByWithCount = CoalesceBy, (usize, ::Item)>; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct DedupPredWithCount2CoalescePred(DP); impl CoalescePredicate for DedupPredWithCount2CoalescePred diff --git a/src/adaptors/map.rs b/src/adaptors/map.rs index eee5cc35f..00ff51629 100644 --- a/src/adaptors/map.rs +++ b/src/adaptors/map.rs @@ -1,7 +1,7 @@ use std::iter::FromIterator; use std::marker::PhantomData; -#[derive(Clone)] +#[derive(Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct MapSpecialCase { iter: I, @@ -108,7 +108,7 @@ impl, U> MapSpecialCaseFn for MapSpecialCaseFnInto { } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct MapSpecialCaseFnInto(PhantomData); /// Create a new [`MapInto`] iterator. diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index bd0d93a5f..9bacf8d5c 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -477,7 +477,7 @@ pub trait MergePredicate { fn merge_pred(&mut self, a: &T, b: &T) -> bool; } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct MergeLte; impl MergePredicate for MergeLte { diff --git a/src/duplicates_impl.rs b/src/duplicates_impl.rs index d2a0d2b3f..75676aefb 100644 --- a/src/duplicates_impl.rs +++ b/src/duplicates_impl.rs @@ -122,6 +122,7 @@ mod private { } /// Apply the identity function to elements before checking them for equality. + #[derive(Debug)] pub struct ById; impl KeyMethod for ById { type Container = JustValue; @@ -152,6 +153,7 @@ mod private { fn value(self) -> V; } + #[derive(Debug)] pub struct KeyValue(K, V); impl KeyXorValue for KeyValue { fn key_ref(&self) -> &K { @@ -165,6 +167,7 @@ mod private { } } + #[derive(Debug)] pub struct JustValue(V); impl KeyXorValue for JustValue { fn key_ref(&self) -> &V { diff --git a/src/kmerge_impl.rs b/src/kmerge_impl.rs index dce5b782c..bd56b0317 100644 --- a/src/kmerge_impl.rs +++ b/src/kmerge_impl.rs @@ -111,7 +111,7 @@ pub trait KMergePredicate { fn kmerge_pred(&mut self, a: &T, b: &T) -> bool; } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct KMergeByLt; impl KMergePredicate for KMergeByLt { diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index e225f3d34..d914e0323 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -77,7 +77,7 @@ impl ExactSizeIterator for TupleBuffer /// An iterator that groups the items in tuples of a specific size. /// /// See [`.tuples()`](crate::Itertools::tuples) for more information. -#[derive(Clone)] +#[derive(Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct Tuples where I: Iterator, From b055dc94ba172c5e6f33d00a4b5ad1be5b9f4958 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 20 Aug 2021 15:39:27 +0200 Subject: [PATCH 2/3] impl `Debug` (2) some debug_fmt_fields --- src/adaptors/coalesce.rs | 4 ++++ src/adaptors/map.rs | 4 ++++ src/adaptors/mod.rs | 28 ++++++++++++++++++++++++++++ src/duplicates_impl.rs | 3 +++ src/pad_tail.rs | 7 +++++++ src/peeking_take_while.rs | 7 +++++++ 6 files changed, 53 insertions(+) diff --git a/src/adaptors/coalesce.rs b/src/adaptors/coalesce.rs index f0cddd20e..b1aff6e27 100644 --- a/src/adaptors/coalesce.rs +++ b/src/adaptors/coalesce.rs @@ -119,6 +119,10 @@ pub type DedupBy = CoalesceBy, (DP); +impl fmt::Debug for DedupPred2CoalescePred { + debug_fmt_fields!(DedupPred2CoalescePred,); +} + pub trait DedupPredicate { // TODO replace by Fn(&T, &T)->bool once Rust supports it fn dedup_pair(&mut self, a: &T, b: &T) -> bool; diff --git a/src/adaptors/map.rs b/src/adaptors/map.rs index 00ff51629..cf5e5a00d 100644 --- a/src/adaptors/map.rs +++ b/src/adaptors/map.rs @@ -84,6 +84,10 @@ where #[derive(Clone)] pub struct MapSpecialCaseFnOk(F); +impl std::fmt::Debug for MapSpecialCaseFnOk { + debug_fmt_fields!(MapSpecialCaseFnOk,); +} + /// Create a new `MapOk` iterator. pub fn map_ok(iter: I, f: F) -> MapOk where diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 9bacf8d5c..2010f535b 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -849,6 +849,13 @@ pub struct FilterOk { f: F } +impl fmt::Debug for FilterOk +where + I: fmt::Debug, +{ + debug_fmt_fields!(FilterOk, iter); +} + /// Create a new `FilterOk` iterator. pub fn filter_ok(iter: I, f: F) -> FilterOk where I: Iterator>, @@ -917,6 +924,13 @@ pub struct FilterMapOk { f: F } +impl fmt::Debug for FilterMapOk +where + I: fmt::Debug, +{ + debug_fmt_fields!(FilterMapOk, iter); +} + fn transpose_result(result: Result, E>) -> Option> { match result { Ok(Some(v)) => Some(Ok(v)), @@ -995,6 +1009,13 @@ pub struct Positions { count: usize, } +impl fmt::Debug for Positions +where + I: fmt::Debug, +{ + debug_fmt_fields!(Positions, iter, count); +} + /// Create a new `Positions` iterator. pub fn positions(iter: I, f: F) -> Positions where I: Iterator, @@ -1058,6 +1079,13 @@ pub struct Update { f: F, } +impl fmt::Debug for Update +where + I: fmt::Debug, +{ + debug_fmt_fields!(Update, iter); +} + /// Create a new `Update` iterator. pub fn update(iter: I, f: F) -> Update where diff --git a/src/duplicates_impl.rs b/src/duplicates_impl.rs index 75676aefb..640d4818c 100644 --- a/src/duplicates_impl.rs +++ b/src/duplicates_impl.rs @@ -134,6 +134,9 @@ mod private { /// Apply a user-supplied function to elements before checking them for equality. pub struct ByFn(pub(crate) F); + impl fmt::Debug for ByFn { + debug_fmt_fields!(ByFn,); + } impl KeyMethod for ByFn where F: FnMut(&V) -> K, diff --git a/src/pad_tail.rs b/src/pad_tail.rs index 03867cbf0..de57ee416 100644 --- a/src/pad_tail.rs +++ b/src/pad_tail.rs @@ -16,6 +16,13 @@ pub struct PadUsing { filler: F, } +impl std::fmt::Debug for PadUsing +where + I: std::fmt::Debug, +{ + debug_fmt_fields!(PadUsing, iter, min, pos); +} + /// Create a new **PadUsing** iterator. pub fn pad_using(iter: I, min: usize, filler: F) -> PadUsing where I: Iterator, diff --git a/src/peeking_take_while.rs b/src/peeking_take_while.rs index f9f213421..cd0945a52 100644 --- a/src/peeking_take_while.rs +++ b/src/peeking_take_while.rs @@ -83,6 +83,13 @@ pub struct PeekingTakeWhile<'a, I: 'a, F> f: F, } +impl<'a, I: 'a, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F> +where + I: Iterator + std::fmt::Debug, +{ + debug_fmt_fields!(PeekingTakeWhile, iter); +} + /// Create a PeekingTakeWhile pub fn peeking_take_while(iter: &mut I, f: F) -> PeekingTakeWhile where I: Iterator, From a5ac115c02cd5fb55503f11105535102567e1be6 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 20 Aug 2021 15:39:27 +0200 Subject: [PATCH 3/3] impl `Debug` (3) MultiProduct This requires that debug_fmt_fields can also cope with tuple elements. --- src/adaptors/multi_product.rs | 8 ++++++++ src/impl_macros.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index 9708ef4bd..30650eda6 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -18,6 +18,14 @@ pub struct MultiProduct(Vec>) where I: Iterator + Clone, I::Item: Clone; +impl std::fmt::Debug for MultiProduct +where + I: Iterator + Clone + std::fmt::Debug, + I::Item: Clone + std::fmt::Debug, +{ + debug_fmt_fields!(CoalesceBy, 0); +} + /// Create a new cartesian product iterator over an arbitrary number /// of iterators of the same type. /// diff --git a/src/impl_macros.rs b/src/impl_macros.rs index 3da8c632f..5772baeb6 100644 --- a/src/impl_macros.rs +++ b/src/impl_macros.rs @@ -2,7 +2,7 @@ //! Implementation's internal macros macro_rules! debug_fmt_fields { - ($tyname:ident, $($($field:ident).+),*) => { + ($tyname:ident, $($($field:tt/*TODO ideally we would accept ident or tuple element here*/).+),*) => { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { f.debug_struct(stringify!($tyname)) $(