Skip to content

Commit 890d52c

Browse files
goffrietaiki-e
authored andcommitted
Override Debug for oneshot::{Sender,Receiver}.
Prior to this diff, the Debug impl for oneshot channels printed something like: ``` Sender { inner: Inner { complete: false, data: Lock { locked: false, data: UnsafeCell }, rx_task: Lock { locked: false, data: UnsafeCell }, tx_task: Lock { locked: false, data: UnsafeCell } } } ``` which isn't very helpful. Instead, just print `Sender { complete: false }` (or `true`, if the other end has dropped). Note that the `T: Debug` bound is retained to allow for the possibility of allowing slightly more interesting debug output in the future.
1 parent b54f9a9 commit 890d52c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

futures-channel/src/oneshot.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ use crate::lock::Lock;
1616
///
1717
/// This is created by the [`channel`](channel) function.
1818
#[must_use = "futures do nothing unless you `.await` or poll them"]
19-
#[derive(Debug)]
2019
pub struct Receiver<T> {
2120
inner: Arc<Inner<T>>,
2221
}
2322

2423
/// A means of transmitting a single value to another task.
2524
///
2625
/// This is created by the [`channel`](channel) function.
27-
#[derive(Debug)]
2826
pub struct Sender<T> {
2927
inner: Arc<Inner<T>>,
3028
}
@@ -35,7 +33,6 @@ impl<T> Unpin for Sender<T> {}
3533

3634
/// Internal state of the `Receiver`/`Sender` pair above. This is all used as
3735
/// the internal synchronization between the two for send/recv operations.
38-
#[derive(Debug)]
3936
struct Inner<T> {
4037
/// Indicates whether this oneshot is complete yet. This is filled in both
4138
/// by `Sender::drop` and by `Receiver::drop`, and both sides interpret it
@@ -394,6 +391,12 @@ impl<T> Drop for Sender<T> {
394391
}
395392
}
396393

394+
impl<T: fmt::Debug> fmt::Debug for Sender<T> {
395+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
396+
f.debug_struct("Sender").field("complete", &self.inner.complete).finish()
397+
}
398+
}
399+
397400
/// A future that resolves when the receiving end of a channel has hung up.
398401
///
399402
/// This is an `.await`-friendly interface around [`poll_canceled`](Sender::poll_canceled).
@@ -481,3 +484,9 @@ impl<T> Drop for Receiver<T> {
481484
self.inner.drop_rx()
482485
}
483486
}
487+
488+
impl<T: fmt::Debug> fmt::Debug for Receiver<T> {
489+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
490+
f.debug_struct("Receiver").field("complete", &self.inner.complete).finish()
491+
}
492+
}

futures/tests/oneshot.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ fn oneshot_drop_rx() {
6464
drop(rx);
6565
assert_eq!(Err(2), tx.send(2));
6666
}
67+
68+
#[test]
69+
fn oneshot_debug() {
70+
let (tx, rx) = oneshot::channel::<i32>();
71+
assert_eq!(format!("{:?}", tx), "Sender { complete: false }");
72+
assert_eq!(format!("{:?}", rx), "Receiver { complete: false }");
73+
drop(rx);
74+
assert_eq!(format!("{:?}", tx), "Sender { complete: true }");
75+
let (tx, rx) = oneshot::channel::<i32>();
76+
drop(tx);
77+
assert_eq!(format!("{:?}", rx), "Receiver { complete: true }");
78+
}

0 commit comments

Comments
 (0)