summaryrefslogtreecommitdiffstats
path: root/tokio
diff options
context:
space:
mode:
authoryim7 <yimchiu7@gmail.com>2019-12-18 14:02:31 +0800
committerCarl Lerche <me@carllerche.com>2019-12-17 22:02:31 -0800
commite5b99b0f7a12ca27b390535b8628f87a61a08eb6 (patch)
treef4fcd3b997b9a7b5ca4f7233ea6469204d5285a9 /tokio
parent83cd754bc80dc8718b65fd32f54e53b4d7ba8332 (diff)
sync: print MutexGuard inner value for debugging (#1961)
Diffstat (limited to 'tokio')
-rw-r--r--tokio/src/sync/mutex.rs7
-rw-r--r--tokio/tests/sync_mutex.rs13
2 files changed, 15 insertions, 5 deletions
diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs
index 35e13300..bee00df4 100644
--- a/tokio/src/sync/mutex.rs
+++ b/tokio/src/sync/mutex.rs
@@ -61,7 +61,6 @@ pub struct Mutex<T> {
///
/// The lock is automatically released whenever the guard is dropped, at which point `lock`
/// will succeed yet again.
-#[derive(Debug)]
pub struct MutexGuard<'a, T> {
lock: &'a Mutex<T>,
permit: semaphore::Permit,
@@ -176,6 +175,12 @@ impl<'a, T> DerefMut for MutexGuard<'a, T> {
}
}
+impl<'a, T: fmt::Debug> fmt::Debug for MutexGuard<'a, T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
impl<'a, T: fmt::Display> fmt::Display for MutexGuard<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
diff --git a/tokio/tests/sync_mutex.rs b/tokio/tests/sync_mutex.rs
index d803bd59..444ebd6a 100644
--- a/tokio/tests/sync_mutex.rs
+++ b/tokio/tests/sync_mutex.rs
@@ -82,8 +82,7 @@ fn lock() {
}
*/
-#[tokio::main]
-#[test]
+#[tokio::test]
/// Ensure a mutex is unlocked if a future holding the lock
/// is aborted prematurely.
async fn aborted_future_1() {
@@ -108,8 +107,7 @@ async fn aborted_future_1() {
.expect("Mutex is locked");
}
-#[tokio::main]
-#[test]
+#[tokio::test]
/// This test is similar to `aborted_future_1` but this time the
/// aborted future is waiting for the lock.
async fn aborted_future_2() {
@@ -147,3 +145,10 @@ fn try_lock() {
let g3 = m.try_lock();
assert_eq!(g3.is_ok(), true);
}
+
+#[tokio::test]
+async fn debug_format() {
+ let s = "debug";
+ let m = Mutex::new(s.to_string());
+ assert_eq!(format!("{:?}", s), format!("{:?}", m.lock().await));
+}