Skip to content

Commit a139571

Browse files
bors[bot]phimuemue
andauthored
Merge #573
573: Debug impls r=phimuemue a=phimuemue To solve #32 a bit further, this adds some `Debug` impls, following our established guidelines (see #268 (comment)). * `derive(Debug)` on structs without closures. * `debug_fmt_fields` on structs with closures. I extended `debug_fmt_fields` to support debug-formatting tuple elemts. This does not yet cover all cases, but a good fraction of them. Co-authored-by: philipp <descpl@yahoo.de>
2 parents 4ac4f43 + a5ac115 commit a139571

File tree

10 files changed

+72
-8
lines changed

10 files changed

+72
-8
lines changed

src/adaptors/coalesce.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ pub type DedupBy<I, Pred> = CoalesceBy<I, DedupPred2CoalescePred<Pred>, <I as It
119119
#[derive(Clone)]
120120
pub struct DedupPred2CoalescePred<DP>(DP);
121121

122+
impl<DP> fmt::Debug for DedupPred2CoalescePred<DP> {
123+
debug_fmt_fields!(DedupPred2CoalescePred,);
124+
}
125+
122126
pub trait DedupPredicate<T> {
123127
// TODO replace by Fn(&T, &T)->bool once Rust supports it
124128
fn dedup_pair(&mut self, a: &T, b: &T) -> bool;
@@ -137,7 +141,7 @@ where
137141
}
138142
}
139143

140-
#[derive(Clone)]
144+
#[derive(Clone, Debug)]
141145
pub struct DedupEq;
142146

143147
impl<T: PartialEq> DedupPredicate<T> for DedupEq {
@@ -186,7 +190,7 @@ where
186190
pub type DedupByWithCount<I, Pred> =
187191
CoalesceBy<I, DedupPredWithCount2CoalescePred<Pred>, (usize, <I as Iterator>::Item)>;
188192

189-
#[derive(Clone)]
193+
#[derive(Clone, Debug)]
190194
pub struct DedupPredWithCount2CoalescePred<DP>(DP);
191195

192196
impl<DP, T> CoalescePredicate<T, (usize, T)> for DedupPredWithCount2CoalescePred<DP>

src/adaptors/map.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter::FromIterator;
22
use std::marker::PhantomData;
33

4-
#[derive(Clone)]
4+
#[derive(Clone, Debug)]
55
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
66
pub struct MapSpecialCase<I, F> {
77
iter: I,
@@ -84,6 +84,10 @@ where
8484
#[derive(Clone)]
8585
pub struct MapSpecialCaseFnOk<F>(F);
8686

87+
impl<F> std::fmt::Debug for MapSpecialCaseFnOk<F> {
88+
debug_fmt_fields!(MapSpecialCaseFnOk,);
89+
}
90+
8791
/// Create a new `MapOk` iterator.
8892
pub fn map_ok<I, F, T, U, E>(iter: I, f: F) -> MapOk<I, F>
8993
where
@@ -108,7 +112,7 @@ impl<T: Into<U>, U> MapSpecialCaseFn<T> for MapSpecialCaseFnInto<U> {
108112
}
109113
}
110114

111-
#[derive(Clone)]
115+
#[derive(Clone, Debug)]
112116
pub struct MapSpecialCaseFnInto<U>(PhantomData<U>);
113117

114118
/// Create a new [`MapInto`] iterator.

src/adaptors/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ pub trait MergePredicate<T> {
477477
fn merge_pred(&mut self, a: &T, b: &T) -> bool;
478478
}
479479

480-
#[derive(Clone)]
480+
#[derive(Clone, Debug)]
481481
pub struct MergeLte;
482482

483483
impl<T: PartialOrd> MergePredicate<T> for MergeLte {
@@ -849,6 +849,13 @@ pub struct FilterOk<I, F> {
849849
f: F
850850
}
851851

852+
impl<I, F> fmt::Debug for FilterOk<I, F>
853+
where
854+
I: fmt::Debug,
855+
{
856+
debug_fmt_fields!(FilterOk, iter);
857+
}
858+
852859
/// Create a new `FilterOk` iterator.
853860
pub fn filter_ok<I, F, T, E>(iter: I, f: F) -> FilterOk<I, F>
854861
where I: Iterator<Item = Result<T, E>>,
@@ -917,6 +924,13 @@ pub struct FilterMapOk<I, F> {
917924
f: F
918925
}
919926

927+
impl<I, F> fmt::Debug for FilterMapOk<I, F>
928+
where
929+
I: fmt::Debug,
930+
{
931+
debug_fmt_fields!(FilterMapOk, iter);
932+
}
933+
920934
fn transpose_result<T, E>(result: Result<Option<T>, E>) -> Option<Result<T, E>> {
921935
match result {
922936
Ok(Some(v)) => Some(Ok(v)),
@@ -995,6 +1009,13 @@ pub struct Positions<I, F> {
9951009
count: usize,
9961010
}
9971011

1012+
impl<I, F> fmt::Debug for Positions<I, F>
1013+
where
1014+
I: fmt::Debug,
1015+
{
1016+
debug_fmt_fields!(Positions, iter, count);
1017+
}
1018+
9981019
/// Create a new `Positions` iterator.
9991020
pub fn positions<I, F>(iter: I, f: F) -> Positions<I, F>
10001021
where I: Iterator,
@@ -1058,6 +1079,13 @@ pub struct Update<I, F> {
10581079
f: F,
10591080
}
10601081

1082+
impl<I, F> fmt::Debug for Update<I, F>
1083+
where
1084+
I: fmt::Debug,
1085+
{
1086+
debug_fmt_fields!(Update, iter);
1087+
}
1088+
10611089
/// Create a new `Update` iterator.
10621090
pub fn update<I, F>(iter: I, f: F) -> Update<I, F>
10631091
where

src/adaptors/multi_product.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ pub struct MultiProduct<I>(Vec<MultiProductIter<I>>)
1818
where I: Iterator + Clone,
1919
I::Item: Clone;
2020

21+
impl<I> std::fmt::Debug for MultiProduct<I>
22+
where
23+
I: Iterator + Clone + std::fmt::Debug,
24+
I::Item: Clone + std::fmt::Debug,
25+
{
26+
debug_fmt_fields!(CoalesceBy, 0);
27+
}
28+
2129
/// Create a new cartesian product iterator over an arbitrary number
2230
/// of iterators of the same type.
2331
///

src/duplicates_impl.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ mod private {
122122
}
123123

124124
/// Apply the identity function to elements before checking them for equality.
125+
#[derive(Debug)]
125126
pub struct ById;
126127
impl<V> KeyMethod<V, V> for ById {
127128
type Container = JustValue<V>;
@@ -133,6 +134,9 @@ mod private {
133134

134135
/// Apply a user-supplied function to elements before checking them for equality.
135136
pub struct ByFn<F>(pub(crate) F);
137+
impl<F> fmt::Debug for ByFn<F> {
138+
debug_fmt_fields!(ByFn,);
139+
}
136140
impl<K, V, F> KeyMethod<K, V> for ByFn<F>
137141
where
138142
F: FnMut(&V) -> K,
@@ -152,6 +156,7 @@ mod private {
152156
fn value(self) -> V;
153157
}
154158

159+
#[derive(Debug)]
155160
pub struct KeyValue<K, V>(K, V);
156161
impl<K, V> KeyXorValue<K, V> for KeyValue<K, V> {
157162
fn key_ref(&self) -> &K {
@@ -165,6 +170,7 @@ mod private {
165170
}
166171
}
167172

173+
#[derive(Debug)]
168174
pub struct JustValue<V>(V);
169175
impl<V> KeyXorValue<V, V> for JustValue<V> {
170176
fn key_ref(&self) -> &V {

src/impl_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! Implementation's internal macros
33
44
macro_rules! debug_fmt_fields {
5-
($tyname:ident, $($($field:ident).+),*) => {
5+
($tyname:ident, $($($field:tt/*TODO ideally we would accept ident or tuple element here*/).+),*) => {
66
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
77
f.debug_struct(stringify!($tyname))
88
$(

src/kmerge_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub trait KMergePredicate<T> {
111111
fn kmerge_pred(&mut self, a: &T, b: &T) -> bool;
112112
}
113113

114-
#[derive(Clone)]
114+
#[derive(Clone, Debug)]
115115
pub struct KMergeByLt;
116116

117117
impl<T: PartialOrd> KMergePredicate<T> for KMergeByLt {

src/pad_tail.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ pub struct PadUsing<I, F> {
1616
filler: F,
1717
}
1818

19+
impl<I, F> std::fmt::Debug for PadUsing<I, F>
20+
where
21+
I: std::fmt::Debug,
22+
{
23+
debug_fmt_fields!(PadUsing, iter, min, pos);
24+
}
25+
1926
/// Create a new **PadUsing** iterator.
2027
pub fn pad_using<I, F>(iter: I, min: usize, filler: F) -> PadUsing<I, F>
2128
where I: Iterator,

src/peeking_take_while.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ pub struct PeekingTakeWhile<'a, I: 'a, F>
8383
f: F,
8484
}
8585

86+
impl<'a, I: 'a, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F>
87+
where
88+
I: Iterator + std::fmt::Debug,
89+
{
90+
debug_fmt_fields!(PeekingTakeWhile, iter);
91+
}
92+
8693
/// Create a PeekingTakeWhile
8794
pub fn peeking_take_while<I, F>(iter: &mut I, f: F) -> PeekingTakeWhile<I, F>
8895
where I: Iterator,

src/tuple_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<T> ExactSizeIterator for TupleBuffer<T>
7777
/// An iterator that groups the items in tuples of a specific size.
7878
///
7979
/// See [`.tuples()`](crate::Itertools::tuples) for more information.
80-
#[derive(Clone)]
80+
#[derive(Clone, Debug)]
8181
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
8282
pub struct Tuples<I, T>
8383
where I: Iterator<Item = T::Item>,

0 commit comments

Comments
 (0)