Skip to content

Commit 0fb518a

Browse files
oxalicaseanmonstar
authored andcommitted
Avoid infinite recursion in Debug of MutexGuard
1 parent a3e5962 commit 0fb518a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

futures-util/src/lock/mutex.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub struct MutexGuard<'a, T: ?Sized> {
246246
impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
247247
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248248
f.debug_struct("MutexGuard")
249-
.field("value", &*self)
249+
.field("value", &&**self)
250250
.field("mutex", &self.mutex)
251251
.finish()
252252
}
@@ -292,3 +292,10 @@ unsafe impl<T: ?Sized> Sync for MutexLockFuture<'_, T> {}
292292
// lock is essentially spinlock-equivalent (attempt to flip an atomic bool)
293293
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
294294
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
295+
296+
#[test]
297+
fn test_mutex_guard_debug_not_recurse() {
298+
let mutex = Mutex::new(42);
299+
let guard = mutex.try_lock().unwrap();
300+
let _ = format!("{:?}", guard);
301+
}

0 commit comments

Comments
 (0)