Skip to content

Commit c85d1e8

Browse files
authored
refactor: reduce noise in fmt::Debug outputs (#855)
1 parent f36a032 commit c85d1e8

File tree

4 files changed

+104
-23
lines changed

4 files changed

+104
-23
lines changed

src/proto/streams/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,56 @@ pub struct Config {
7373
/// When this gets exceeded, we issue GOAWAYs.
7474
pub local_max_error_reset_streams: Option<usize>,
7575
}
76+
77+
trait DebugStructExt<'a, 'b> {
78+
// h2_ prefixes to protect against possible future name collisions
79+
fn h2_field_if(&mut self, name: &str, val: &bool) -> &mut std::fmt::DebugStruct<'a, 'b>;
80+
81+
fn h2_field_if_then<T: std::fmt::Debug>(
82+
&mut self,
83+
name: &str,
84+
cond: bool,
85+
val: &T,
86+
) -> &mut std::fmt::DebugStruct<'a, 'b>;
87+
88+
fn h2_field_some<T: std::fmt::Debug>(
89+
&mut self,
90+
name: &str,
91+
val: &Option<T>,
92+
) -> &mut std::fmt::DebugStruct<'a, 'b>;
93+
}
94+
95+
impl<'a, 'b> DebugStructExt<'a, 'b> for std::fmt::DebugStruct<'a, 'b> {
96+
fn h2_field_if(&mut self, name: &str, val: &bool) -> &mut std::fmt::DebugStruct<'a, 'b> {
97+
if *val {
98+
self.field(name, val)
99+
} else {
100+
self
101+
}
102+
}
103+
104+
fn h2_field_if_then<T: std::fmt::Debug>(
105+
&mut self,
106+
name: &str,
107+
cond: bool,
108+
val: &T,
109+
) -> &mut std::fmt::DebugStruct<'a, 'b> {
110+
if cond {
111+
self.field(name, val)
112+
} else {
113+
self
114+
}
115+
}
116+
117+
fn h2_field_some<T: std::fmt::Debug>(
118+
&mut self,
119+
name: &str,
120+
val: &Option<T>,
121+
) -> &mut std::fmt::DebugStruct<'a, 'b> {
122+
if val.is_some() {
123+
self.field(name, val)
124+
} else {
125+
self
126+
}
127+
}
128+
}

src/proto/streams/state.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::io;
23

34
use crate::codec::UserError;
@@ -47,7 +48,7 @@ use self::Peer::*;
4748
/// ES: END_STREAM flag
4849
/// R: RST_STREAM frame
4950
/// ```
50-
#[derive(Debug, Clone)]
51+
#[derive(Clone)]
5152
pub struct State {
5253
inner: Inner,
5354
}
@@ -465,3 +466,10 @@ impl Default for State {
465466
State { inner: Inner::Idle }
466467
}
467468
}
469+
470+
// remove some noise for debug output
471+
impl fmt::Debug for State {
472+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
473+
self.inner.fmt(f)
474+
}
475+
}

src/proto/streams/store.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub(crate) struct Key {
3434
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3535
struct SlabIndex(u32);
3636

37-
#[derive(Debug)]
3837
pub(super) struct Queue<N> {
3938
indices: Option<store::Indices>,
4039
_p: PhantomData<N>,
@@ -378,6 +377,15 @@ where
378377
}
379378
}
380379

380+
impl<N> fmt::Debug for Queue<N> {
381+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
382+
f.debug_struct("Queue")
383+
.field("indices", &self.indices)
384+
// skip phantom data
385+
.finish()
386+
}
387+
}
388+
381389
// ===== impl Ptr =====
382390

383391
impl<'a> Ptr<'a> {

src/proto/streams/stream.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,35 +398,47 @@ impl fmt::Debug for Stream {
398398
.field("state", &self.state)
399399
.field("is_counted", &self.is_counted)
400400
.field("ref_count", &self.ref_count)
401-
.field("next_pending_send", &self.next_pending_send)
402-
.field("is_pending_send", &self.is_pending_send)
401+
.h2_field_some("next_pending_send", &self.next_pending_send)
402+
.h2_field_if("is_pending_send", &self.is_pending_send)
403403
.field("send_flow", &self.send_flow)
404404
.field("requested_send_capacity", &self.requested_send_capacity)
405405
.field("buffered_send_data", &self.buffered_send_data)
406-
.field("send_task", &self.send_task.as_ref().map(|_| ()))
407-
.field("pending_send", &self.pending_send)
408-
.field(
406+
.h2_field_some("send_task", &self.send_task.as_ref().map(|_| ()))
407+
.h2_field_if_then(
408+
"pending_send",
409+
!self.pending_send.is_empty(),
410+
&self.pending_send,
411+
)
412+
.h2_field_some(
409413
"next_pending_send_capacity",
410414
&self.next_pending_send_capacity,
411415
)
412-
.field("is_pending_send_capacity", &self.is_pending_send_capacity)
413-
.field("send_capacity_inc", &self.send_capacity_inc)
414-
.field("next_open", &self.next_open)
415-
.field("is_pending_open", &self.is_pending_open)
416-
.field("is_pending_push", &self.is_pending_push)
417-
.field("next_pending_accept", &self.next_pending_accept)
418-
.field("is_pending_accept", &self.is_pending_accept)
416+
.h2_field_if("is_pending_send_capacity", &self.is_pending_send_capacity)
417+
.h2_field_if("send_capacity_inc", &self.send_capacity_inc)
418+
.h2_field_some("next_open", &self.next_open)
419+
.h2_field_if("is_pending_open", &self.is_pending_open)
420+
.h2_field_if("is_pending_push", &self.is_pending_push)
421+
.h2_field_some("next_pending_accept", &self.next_pending_accept)
422+
.h2_field_if("is_pending_accept", &self.is_pending_accept)
419423
.field("recv_flow", &self.recv_flow)
420424
.field("in_flight_recv_data", &self.in_flight_recv_data)
421-
.field("next_window_update", &self.next_window_update)
422-
.field("is_pending_window_update", &self.is_pending_window_update)
423-
.field("reset_at", &self.reset_at)
424-
.field("next_reset_expire", &self.next_reset_expire)
425-
.field("pending_recv", &self.pending_recv)
426-
.field("is_recv", &self.is_recv)
427-
.field("recv_task", &self.recv_task.as_ref().map(|_| ()))
428-
.field("push_task", &self.push_task.as_ref().map(|_| ()))
429-
.field("pending_push_promises", &self.pending_push_promises)
425+
.h2_field_some("next_window_update", &self.next_window_update)
426+
.h2_field_if("is_pending_window_update", &self.is_pending_window_update)
427+
.h2_field_some("reset_at", &self.reset_at)
428+
.h2_field_some("next_reset_expire", &self.next_reset_expire)
429+
.h2_field_if_then(
430+
"pending_recv",
431+
!self.pending_recv.is_empty(),
432+
&self.pending_recv,
433+
)
434+
.h2_field_if("is_recv", &self.is_recv)
435+
.h2_field_some("recv_task", &self.recv_task.as_ref().map(|_| ()))
436+
.h2_field_some("push_task", &self.push_task.as_ref().map(|_| ()))
437+
.h2_field_if_then(
438+
"pending_push_promises",
439+
!self.pending_push_promises.is_empty(),
440+
&self.pending_push_promises,
441+
)
430442
.field("content_length", &self.content_length)
431443
.finish()
432444
}

0 commit comments

Comments
 (0)