Skip to content

Commit

Permalink
Avoid infinite recursion in Debug of MutexGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalica authored and seanmonstar committed Oct 11, 2019
1 parent a3e5962 commit 0fb518a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion futures-util/src/lock/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub struct MutexGuard<'a, T: ?Sized> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MutexGuard")
.field("value", &*self)
.field("value", &&**self)
.field("mutex", &self.mutex)
.finish()
}
Expand Down Expand Up @@ -292,3 +292,10 @@ unsafe impl<T: ?Sized> Sync for MutexLockFuture<'_, T> {}
// lock is essentially spinlock-equivalent (attempt to flip an atomic bool)
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}

#[test]
fn test_mutex_guard_debug_not_recurse() {
let mutex = Mutex::new(42);
let guard = mutex.try_lock().unwrap();
let _ = format!("{:?}", guard);
}

0 comments on commit 0fb518a

Please sign in to comment.