sync: print MutexGuard inner value for debugging (#1961)

This commit is contained in:
yim7
2019-12-17 22:02:31 -08:00
committed by Carl Lerche
parent 83cd754bc8
commit e5b99b0f7a
2 changed files with 15 additions and 5 deletions
+6 -1
View File
@@ -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)
+9 -4
View File
@@ -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));
}