Skip to content

Commit be27d72

Browse files
authored
Custom Debug implementations for mpsc (#2667)
1 parent c938001 commit be27d72

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

futures-channel/src/mpsc/mod.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ mod queue;
9494
#[cfg(feature = "sink")]
9595
mod sink_impl;
9696

97-
#[derive(Debug)]
9897
struct UnboundedSenderInner<T> {
9998
// Channel state shared between the sender and receiver.
10099
inner: Arc<UnboundedInner<T>>,
101100
}
102101

103-
#[derive(Debug)]
104102
struct BoundedSenderInner<T> {
105103
// Channel state shared between the sender and receiver.
106104
inner: Arc<BoundedInner<T>>,
@@ -122,13 +120,11 @@ impl<T> Unpin for BoundedSenderInner<T> {}
122120
/// The transmission end of a bounded mpsc channel.
123121
///
124122
/// This value is created by the [`channel`](channel) function.
125-
#[derive(Debug)]
126123
pub struct Sender<T>(Option<BoundedSenderInner<T>>);
127124

128125
/// The transmission end of an unbounded mpsc channel.
129126
///
130127
/// This value is created by the [`unbounded`](unbounded) function.
131-
#[derive(Debug)]
132128
pub struct UnboundedSender<T>(Option<UnboundedSenderInner<T>>);
133129

134130
trait AssertKinds: Send + Sync + Clone {}
@@ -137,15 +133,13 @@ impl AssertKinds for UnboundedSender<u32> {}
137133
/// The receiving end of a bounded mpsc channel.
138134
///
139135
/// This value is created by the [`channel`](channel) function.
140-
#[derive(Debug)]
141136
pub struct Receiver<T> {
142137
inner: Option<Arc<BoundedInner<T>>>,
143138
}
144139

145140
/// The receiving end of an unbounded mpsc channel.
146141
///
147142
/// This value is created by the [`unbounded`](unbounded) function.
148-
#[derive(Debug)]
149143
pub struct UnboundedReceiver<T> {
150144
inner: Option<Arc<UnboundedInner<T>>>,
151145
}
@@ -255,7 +249,6 @@ impl fmt::Display for TryRecvError {
255249

256250
impl std::error::Error for TryRecvError {}
257251

258-
#[derive(Debug)]
259252
struct UnboundedInner<T> {
260253
// Internal channel state. Consists of the number of messages stored in the
261254
// channel as well as a flag signalling that the channel is closed.
@@ -271,7 +264,6 @@ struct UnboundedInner<T> {
271264
recv_task: AtomicWaker,
272265
}
273266

274-
#[derive(Debug)]
275267
struct BoundedInner<T> {
276268
// Max buffer size of the channel.
277269
buffer: usize,
@@ -294,7 +286,7 @@ struct BoundedInner<T> {
294286
}
295287

296288
// Struct representation of `Inner::state`.
297-
#[derive(Debug, Clone, Copy)]
289+
#[derive(Clone, Copy)]
298290
struct State {
299291
// `true` when the channel is open
300292
is_open: bool,
@@ -318,7 +310,6 @@ const MAX_CAPACITY: usize = !(OPEN_MASK);
318310
const MAX_BUFFER: usize = MAX_CAPACITY >> 1;
319311

320312
// Sent to the consumer to wake up blocked producers
321-
#[derive(Debug)]
322313
struct SenderTask {
323314
task: Option<Waker>,
324315
is_parked: bool,
@@ -941,6 +932,18 @@ impl<T> Drop for BoundedSenderInner<T> {
941932
}
942933
}
943934

935+
impl<T> fmt::Debug for Sender<T> {
936+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
937+
f.debug_struct("Sender").field("closed", &self.is_closed()).finish()
938+
}
939+
}
940+
941+
impl<T> fmt::Debug for UnboundedSender<T> {
942+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
943+
f.debug_struct("UnboundedSender").field("closed", &self.is_closed()).finish()
944+
}
945+
}
946+
944947
/*
945948
*
946949
* ===== impl Receiver =====
@@ -1101,6 +1104,18 @@ impl<T> Drop for Receiver<T> {
11011104
}
11021105
}
11031106

1107+
impl<T> fmt::Debug for Receiver<T> {
1108+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1109+
let closed = if let Some(ref inner) = self.inner {
1110+
decode_state(inner.state.load(SeqCst)).is_closed()
1111+
} else {
1112+
false
1113+
};
1114+
1115+
f.debug_struct("Receiver").field("closed", &closed).finish()
1116+
}
1117+
}
1118+
11041119
impl<T> UnboundedReceiver<T> {
11051120
/// Closes the receiving half of a channel, without dropping it.
11061121
///
@@ -1233,6 +1248,18 @@ impl<T> Drop for UnboundedReceiver<T> {
12331248
}
12341249
}
12351250

1251+
impl<T> fmt::Debug for UnboundedReceiver<T> {
1252+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1253+
let closed = if let Some(ref inner) = self.inner {
1254+
decode_state(inner.state.load(SeqCst)).is_closed()
1255+
} else {
1256+
false
1257+
};
1258+
1259+
f.debug_struct("Receiver").field("closed", &closed).finish()
1260+
}
1261+
}
1262+
12361263
/*
12371264
*
12381265
* ===== impl Inner =====

futures-channel/src/mpsc/queue.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub(super) enum PopResult<T> {
6161
Inconsistent,
6262
}
6363

64-
#[derive(Debug)]
6564
struct Node<T> {
6665
next: AtomicPtr<Self>,
6766
value: Option<T>,
@@ -70,7 +69,6 @@ struct Node<T> {
7069
/// The multi-producer single-consumer structure. This is not cloneable, but it
7170
/// may be safely shared so long as it is guaranteed that there is only one
7271
/// popper at a time (many pushers are allowed).
73-
#[derive(Debug)]
7472
pub(super) struct Queue<T> {
7573
head: AtomicPtr<Node<T>>,
7674
tail: UnsafeCell<*mut Node<T>>,

0 commit comments

Comments
 (0)