@@ -65,17 +65,48 @@ pub struct Guard {
65
65
/// each lock, but once a lock is poisoned then all future acquisitions will
66
66
/// return this error.
67
67
///
68
+ /// # Examples
69
+ ///
70
+ /// ```
71
+ /// use std::sync::{Arc, Mutex};
72
+ /// use std::thread;
73
+ ///
74
+ /// let mutex = Arc::new(Mutex::new(1));
75
+ ///
76
+ /// // poison the mutex
77
+ /// let c_mutex = mutex.clone();
78
+ /// let _ = thread::spawn(move || {
79
+ /// let mut data = c_mutex.lock().unwrap();
80
+ /// *data = 2;
81
+ /// panic!();
82
+ /// }).join();
83
+ ///
84
+ /// match mutex.lock() {
85
+ /// Ok(_) => unreachable!(),
86
+ /// Err(p_err) => {
87
+ /// let data = p_err.get_ref();
88
+ /// println!("recovered: {}", data);
89
+ /// }
90
+ /// };
91
+ /// ```
92
+ ///
68
93
/// [`Mutex`]: ../../std/sync/struct.Mutex.html
69
94
/// [`RwLock`]: ../../std/sync/struct.RwLock.html
70
95
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
71
96
pub struct PoisonError < T > {
72
97
guard : T ,
73
98
}
74
99
75
- /// An enumeration of possible errors which can occur while calling the
76
- /// [`try_lock`] method.
100
+ /// An enumeration of possible errors associated with a [`TryLockResult`] which
101
+ /// can occur while trying to aquire a lock, from the [`try_lock`] method on a
102
+ /// [`Mutex`] or the [`try_read`] and [`try_write`] methods on an [`RwLock`].
77
103
///
104
+ /// [`Mutex`]: struct.Mutex.html
105
+ /// [`RwLock`]: struct.RwLock.html
106
+ /// [`TryLockResult`]: type.TryLockResult.html
78
107
/// [`try_lock`]: struct.Mutex.html#method.try_lock
108
+ /// [`try_read`]: struct.RwLock.html#method.try_read
109
+ /// [`try_write`]: struct.RwLock.html#method.try_write
79
110
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
80
111
pub enum TryLockError < T > {
81
112
/// The lock could not be acquired because another thread failed while holding
@@ -148,6 +179,28 @@ impl<T> PoisonError<T> {
148
179
149
180
/// Consumes this error indicating that a lock is poisoned, returning the
150
181
/// underlying guard to allow access regardless.
182
+ ///
183
+ /// # Examples
184
+ ///
185
+ /// ```
186
+ /// use std::collections::HashSet;
187
+ /// use std::sync::{Arc, Mutex};
188
+ /// use std::thread;
189
+ ///
190
+ /// let mutex = Arc::new(Mutex::new(HashSet::new()));
191
+ ///
192
+ /// // poison the mutex
193
+ /// let c_mutex = mutex.clone();
194
+ /// let _ = thread::spawn(move || {
195
+ /// let mut data = c_mutex.lock().unwrap();
196
+ /// data.insert(10);
197
+ /// panic!();
198
+ /// }).join();
199
+ ///
200
+ /// let p_err = mutex.lock().unwrap_err();
201
+ /// let data = p_err.into_inner();
202
+ /// println!("recovered {} items", data.len());
203
+ /// ```
151
204
#[ stable( feature = "sync_poison" , since = "1.2.0" ) ]
152
205
pub fn into_inner ( self ) -> T { self . guard }
153
206
0 commit comments