@@ -23,14 +23,15 @@ impl<I> PrettySample<I> {
2323 }
2424}
2525
26- impl < I , T > fmt:: Debug for PrettySample < I >
26+ impl < I > fmt:: Debug for PrettySample < I >
2727where
28- I : IntoIterator < Item = T > + Clone ,
29- T : fmt:: Debug ,
28+ I : IntoIterator + Clone ,
29+ I :: Item : fmt:: Debug ,
3030{
3131 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3232 write ! ( formatter, "[" ) ?;
33- // in general we will get passed a reference (&[...], &HashMap...) or a Map<_> of them.
33+
34+ // In general, we will receive a reference (&[...], &HashMap...) or a Map<_> of them.
3435 // So we either perform a Copy, or a cheap Clone of a simple struct
3536 let mut iter = self . 0 . clone ( ) . into_iter ( ) . enumerate ( ) ;
3637 for ( i, item) in & mut iter {
@@ -55,9 +56,9 @@ pub trait PrettyDisplay {
5556 fn pretty_display ( & self ) -> impl fmt:: Display ;
5657}
5758
58- struct PrettyDurationDisplay < ' a > ( & ' a Duration ) ;
59+ struct DurationPrettyDisplay < ' a > ( & ' a Duration ) ;
5960
60- impl fmt:: Display for PrettyDurationDisplay < ' _ > {
61+ impl fmt:: Display for DurationPrettyDisplay < ' _ > {
6162 fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
6263 // This is enough for my current use cases. To be extended as you see fit.
6364 let duration_millis = self . 0 . as_millis ( ) ;
@@ -76,7 +77,37 @@ impl fmt::Display for PrettyDurationDisplay<'_> {
7677
7778impl PrettyDisplay for Duration {
7879 fn pretty_display ( & self ) -> impl fmt:: Display {
79- PrettyDurationDisplay ( self )
80+ DurationPrettyDisplay ( self )
81+ }
82+ }
83+
84+ struct SequencePrettyDisplay < I > ( I ) ;
85+
86+ impl < I > fmt:: Display for SequencePrettyDisplay < I >
87+ where
88+ I : IntoIterator + Clone ,
89+ I :: Item : fmt:: Display ,
90+ {
91+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
92+ write ! ( f, "[" ) ?;
93+
94+ // In general, we will receive a reference (&[...], &HashMap...) or a Map<_> of them.
95+ // So we either perform a Copy, or a cheap Clone of a simple struct
96+ let mut iter = self . 0 . clone ( ) . into_iter ( ) . peekable ( ) ;
97+
98+ while let Some ( item) = iter. next ( ) {
99+ write ! ( f, "{item}" ) ?;
100+ if iter. peek ( ) . is_some ( ) {
101+ write ! ( f, ", " ) ?;
102+ }
103+ }
104+ write ! ( f, "]" )
105+ }
106+ }
107+
108+ impl < T : fmt:: Display > PrettyDisplay for & [ T ] {
109+ fn pretty_display ( & self ) -> impl fmt:: Display {
110+ SequencePrettyDisplay ( * self )
80111 }
81112}
82113
@@ -103,17 +134,29 @@ mod tests {
103134 }
104135
105136 #[ test]
106- fn test_pretty_duration ( ) {
107- let pretty_duration = Duration :: from_millis ( 0 ) ;
108- assert_eq ! ( format!( "{}" , pretty_duration . pretty_display( ) ) , "0ms" ) ;
137+ fn test_duration_pretty_display ( ) {
138+ let duration = Duration :: from_millis ( 0 ) ;
139+ assert_eq ! ( format!( "{}" , duration . pretty_display( ) ) , "0ms" ) ;
109140
110- let pretty_duration = Duration :: from_millis ( 125 ) ;
111- assert_eq ! ( format!( "{}" , pretty_duration. pretty_display( ) ) , "125ms" ) ;
141+ let duration = Duration :: from_millis ( 125 ) ;
142+ assert_eq ! ( format!( "{}" , duration. pretty_display( ) ) , "125ms" ) ;
143+
144+ let duration = Duration :: from_millis ( 1_000 ) ;
145+ assert_eq ! ( format!( "{}" , duration. pretty_display( ) ) , "1.0s" ) ;
146+
147+ let duration = Duration :: from_millis ( 1_125 ) ;
148+ assert_eq ! ( format!( "{}" , duration. pretty_display( ) ) , "1.12s" ) ;
149+ }
150+
151+ #[ test]
152+ fn test_sequence_pretty_display ( ) {
153+ let empty_slice: & [ i32 ] = & [ ] ;
154+ assert_eq ! ( format!( "{}" , empty_slice. pretty_display( ) ) , "[]" ) ;
112155
113- let pretty_duration = Duration :: from_millis ( 1_000 ) ;
114- assert_eq ! ( format!( "{}" , pretty_duration . pretty_display( ) ) , "1.0s " ) ;
156+ let slice_one : & [ i32 ] = & [ 1 ] ;
157+ assert_eq ! ( format!( "{}" , slice_one . pretty_display( ) ) , "[1] " ) ;
115158
116- let pretty_duration = Duration :: from_millis ( 1_125 ) ;
117- assert_eq ! ( format!( "{}" , pretty_duration . pretty_display( ) ) , "1.12s " ) ;
159+ let slice_two : & [ i32 ] = & [ 1 , 2 ] ;
160+ assert_eq ! ( format!( "{}" , slice_two . pretty_display( ) ) , "[1, 2] " ) ;
118161 }
119162}
0 commit comments