From 3368ca17ee149f2640527ba2b75cfd1304c2faa5 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Thu, 6 May 2021 16:27:01 -0700 Subject: [PATCH] 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. --- futures-channel/src/oneshot.rs | 15 ++++++++++++--- futures/tests/oneshot.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/futures-channel/src/oneshot.rs b/futures-channel/src/oneshot.rs index dbbce8112b..1c393c5a85 100644 --- a/futures-channel/src/oneshot.rs +++ b/futures-channel/src/oneshot.rs @@ -16,7 +16,6 @@ use crate::lock::Lock; /// /// This is created by the [`channel`](channel) function. #[must_use = "futures do nothing unless you `.await` or poll them"] -#[derive(Debug)] pub struct Receiver { inner: Arc>, } @@ -24,7 +23,6 @@ pub struct Receiver { /// A means of transmitting a single value to another task. /// /// This is created by the [`channel`](channel) function. -#[derive(Debug)] pub struct Sender { inner: Arc>, } @@ -35,7 +33,6 @@ impl Unpin for Sender {} /// Internal state of the `Receiver`/`Sender` pair above. This is all used as /// the internal synchronization between the two for send/recv operations. -#[derive(Debug)] struct Inner { /// Indicates whether this oneshot is complete yet. This is filled in both /// by `Sender::drop` and by `Receiver::drop`, and both sides interpret it @@ -394,6 +391,12 @@ impl Drop for Sender { } } +impl fmt::Debug for Sender { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Sender").field("complete", &self.inner.complete).finish() + } +} + /// A future that resolves when the receiving end of a channel has hung up. /// /// This is an `.await`-friendly interface around [`poll_canceled`](Sender::poll_canceled). @@ -481,3 +484,9 @@ impl Drop for Receiver { self.inner.drop_rx() } } + +impl fmt::Debug for Receiver { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Receiver").field("complete", &self.inner.complete).finish() + } +} diff --git a/futures/tests/oneshot.rs b/futures/tests/oneshot.rs index 58951ec581..34b78a33fb 100644 --- a/futures/tests/oneshot.rs +++ b/futures/tests/oneshot.rs @@ -64,3 +64,15 @@ fn oneshot_drop_rx() { drop(rx); assert_eq!(Err(2), tx.send(2)); } + +#[test] +fn oneshot_debug() { + let (tx, rx) = oneshot::channel::(); + assert_eq!(format!("{:?}", tx), "Sender { complete: false }"); + assert_eq!(format!("{:?}", rx), "Receiver { complete: false }"); + drop(rx); + assert_eq!(format!("{:?}", tx), "Sender { complete: true }"); + let (tx, rx) = oneshot::channel::(); + drop(tx); + assert_eq!(format!("{:?}", rx), "Receiver { complete: true }"); +}