Skip to content

Commit b1437bc

Browse files
Rollup merge of rust-lang#44797 - lucasem:master, r=frewsxcv
docs improvement std::sync::{PoisonError, TryLockError} Addresses the `PoisonError` and `TryLockError` parts of rust-lang#29377. Adds examples and links. r? @steveklabnik
2 parents f2feb83 + 99c0c52 commit b1437bc

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

Diff for: src/libstd/sys_common/poison.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,48 @@ pub struct Guard {
6565
/// each lock, but once a lock is poisoned then all future acquisitions will
6666
/// return this error.
6767
///
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+
///
6893
/// [`Mutex`]: ../../std/sync/struct.Mutex.html
6994
/// [`RwLock`]: ../../std/sync/struct.RwLock.html
7095
#[stable(feature = "rust1", since = "1.0.0")]
7196
pub struct PoisonError<T> {
7297
guard: T,
7398
}
7499

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`].
77103
///
104+
/// [`Mutex`]: struct.Mutex.html
105+
/// [`RwLock`]: struct.RwLock.html
106+
/// [`TryLockResult`]: type.TryLockResult.html
78107
/// [`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
79110
#[stable(feature = "rust1", since = "1.0.0")]
80111
pub enum TryLockError<T> {
81112
/// The lock could not be acquired because another thread failed while holding
@@ -148,6 +179,28 @@ impl<T> PoisonError<T> {
148179

149180
/// Consumes this error indicating that a lock is poisoned, returning the
150181
/// 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+
/// ```
151204
#[stable(feature = "sync_poison", since = "1.2.0")]
152205
pub fn into_inner(self) -> T { self.guard }
153206

0 commit comments

Comments
 (0)