diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index c9fbdd1270b..9c305f517ef 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -387,14 +387,19 @@ impl Mutex { /// panic!(); // the mutex gets poisoned /// }).join(); /// - /// let guard = mutex.lock().unwrap_err().into_inner(); - /// Mutex::clear_poison(&guard); + /// assert_eq!(mutex.is_poisoned(), true); + /// let x = mutex.lock().unwrap_or_else(|mut e| { + /// **e.get_mut() = 1; + /// mutex.clear_poison(); + /// e.into_inner() + /// }); /// assert_eq!(mutex.is_poisoned(), false); + /// assert_eq!(*x, 1); /// ``` #[inline] #[unstable(feature = "mutex_unpoison", issue = "96469")] - pub fn clear_poison(guard: &MutexGuard<'_, T>) { - guard.lock.poison.clear(); + pub fn clear_poison(&self) { + self.poison.clear(); } /// Consumes this mutex, returning the underlying data. diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index d068b15dbca..c0cd02708a7 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -390,14 +390,19 @@ impl RwLock { /// panic!(); // the mutex gets poisoned /// }).join(); /// - /// let guard = lock.write().unwrap_err().into_inner(); - /// RwLock::clear_poison(&guard); + /// assert_eq!(lock.is_poisoned(), true); + /// let guard = lock.write().unwrap_or_else(|mut e| { + /// **e.get_mut() = 1; + /// lock.clear_poison(); + /// e.into_inner() + /// }); /// assert_eq!(lock.is_poisoned(), false); + /// assert_eq!(*guard, 1); /// ``` #[inline] #[unstable(feature = "mutex_unpoison", issue = "96469")] - pub fn clear_poison(guard: &RwLockWriteGuard<'_, T>) { - guard.lock.poison.clear(); + pub fn clear_poison(&self) { + self.poison.clear(); } /// Consumes this `RwLock`, returning the underlying data.