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

Better Debug for Mutex #2725

Merged
merged 4 commits into from
Jul 31, 2020
Merged
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion tokio/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ use std::sync::Arc;
/// [`std::sync::Mutex`]: struct@std::sync::Mutex
/// [`Send`]: trait@std::marker::Send
/// [`lock`]: method@Mutex::lock
#[derive(Debug)]
pub struct Mutex<T: ?Sized> {
s: semaphore::Semaphore,
c: UnsafeCell<T>,
Expand Down Expand Up @@ -373,6 +372,18 @@ where
}
}

impl<T> std::fmt::Debug for Mutex<T>
where
T: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.try_lock() {
Ok(inner) => f.debug_struct("Mutex").field("inner", &*inner).finish(),
Err(_) => f.debug_struct("Mutex").field("inner", &"<locked>").finish(),
MikailBag marked this conversation as resolved.
Show resolved Hide resolved
MikailBag marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// === impl MutexGuard ===

impl<T: ?Sized> Drop for MutexGuard<'_, T> {
Expand Down