Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Debug for oneshot::{Sender,Receiver}. #2414

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions futures-channel/src/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ 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<T> {
inner: Arc<Inner<T>>,
}

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

/// 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<T> {
/// Indicates whether this oneshot is complete yet. This is filled in both
/// by `Sender::drop` and by `Receiver::drop`, and both sides interpret it
Expand Down Expand Up @@ -394,6 +391,12 @@ impl<T> Drop for Sender<T> {
}
}

impl<T: fmt::Debug> fmt::Debug for Sender<T> {
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).
Expand Down Expand Up @@ -481,3 +484,9 @@ impl<T> Drop for Receiver<T> {
self.inner.drop_rx()
}
}

impl<T: fmt::Debug> fmt::Debug for Receiver<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Receiver").field("complete", &self.inner.complete).finish()
}
}
12 changes: 12 additions & 0 deletions futures/tests/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>();
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::<i32>();
drop(tx);
assert_eq!(format!("{:?}", rx), "Receiver { complete: true }");
}