Skip to content

Commit 37922fc

Browse files
committed
add poisoning documentation to LazyCell
1 parent cf3865f commit 37922fc

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

library/core/src/cell/lazy.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ enum State<T, F> {
1515
///
1616
/// [`std::sync::LazyLock`]: ../../std/sync/struct.LazyLock.html
1717
///
18+
/// # Poisoning
19+
///
20+
/// If the initialization closure passed to [`LazyCell::new`] panics, the cell will be poisoned.
21+
/// Once the cell is poisoned, any threads that attempt to access this cell (via a dereference
22+
/// or via an explicit call to [`force()`]) will panic.
23+
///
24+
/// This concept is similar to that of poisoning in the [`std::sync::poison`] module. A key
25+
/// difference, however, is that poisoning in `LazyCell` is _unrecoverable_. All future accesses of
26+
/// the cell from other threads will panic, whereas a type in [`std::sync::poison`] like
27+
/// [`std::sync::poison::Mutex`] allows recovery via [`PoisonError::into_inner()`].
28+
///
29+
/// [`force()`]: LazyCell::force
30+
/// [`std::sync::poison`]: ../../std/sync/poison/index.html
31+
/// [`std::sync::poison::Mutex`]: ../../std/sync/poison/struct.Mutex.html
32+
/// [`PoisonError::into_inner()`]: ../../std/sync/poison/struct.PoisonError.html#method.into_inner
33+
///
1834
/// # Examples
1935
///
2036
/// ```
@@ -64,6 +80,10 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
6480
///
6581
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
6682
///
83+
/// # Panics
84+
///
85+
/// Panics if the cell is poisoned.
86+
///
6787
/// # Examples
6888
///
6989
/// ```
@@ -93,6 +113,15 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
93113
///
94114
/// This is equivalent to the `Deref` impl, but is explicit.
95115
///
116+
/// # Panics
117+
///
118+
/// If the initialization closure panics (the one that is passed to the [`new()`] method), the
119+
/// panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
120+
/// accesses of the cell (via [`force()`] or a dereference) to panic.
121+
///
122+
/// [`new()`]: LazyCell::new
123+
/// [`force()`]: LazyCell::force
124+
///
96125
/// # Examples
97126
///
98127
/// ```
@@ -123,6 +152,15 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
123152
/// Forces the evaluation of this lazy value and returns a mutable reference to
124153
/// the result.
125154
///
155+
/// # Panics
156+
///
157+
/// If the initialization closure panics (the one that is passed to the [`new()`] method), the
158+
/// panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
159+
/// accesses of the cell (via [`force()`] or a dereference) to panic.
160+
///
161+
/// [`new()`]: LazyCell::new
162+
/// [`force()`]: LazyCell::force
163+
///
126164
/// # Examples
127165
///
128166
/// ```
@@ -219,7 +257,8 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
219257
}
220258

221259
impl<T, F> LazyCell<T, F> {
222-
/// Returns a mutable reference to the value if initialized, or `None` if not.
260+
/// Returns a mutable reference to the value if initialized. Otherwise (if uninitialized or
261+
/// poisoned), returns `None`.
223262
///
224263
/// # Examples
225264
///
@@ -245,7 +284,8 @@ impl<T, F> LazyCell<T, F> {
245284
}
246285
}
247286

248-
/// Returns a reference to the value if initialized, or `None` if not.
287+
/// Returns a reference to the value if initialized. Otherwise (if uninitialized or poisoned),
288+
/// returns `None`.
249289
///
250290
/// # Examples
251291
///
@@ -278,6 +318,15 @@ impl<T, F> LazyCell<T, F> {
278318
#[stable(feature = "lazy_cell", since = "1.80.0")]
279319
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
280320
type Target = T;
321+
322+
/// # Panics
323+
///
324+
/// If the initialization closure panics (the one that is passed to the [`new()`] method), the
325+
/// panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
326+
/// accesses of the cell (via [`force()`] or a dereference) to panic.
327+
///
328+
/// [`new()`]: LazyCell::new
329+
/// [`force()`]: LazyCell::force
281330
#[inline]
282331
fn deref(&self) -> &T {
283332
LazyCell::force(self)
@@ -286,6 +335,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
286335

287336
#[stable(feature = "lazy_deref_mut", since = "1.89.0")]
288337
impl<T, F: FnOnce() -> T> DerefMut for LazyCell<T, F> {
338+
/// # Panics
339+
///
340+
/// If the initialization closure panics (the one that is passed to the [`new()`] method), the
341+
/// panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
342+
/// accesses of the cell (via [`force()`] or a dereference) to panic.
343+
///
344+
/// [`new()`]: LazyCell::new
345+
/// [`force()`]: LazyCell::force
289346
#[inline]
290347
fn deref_mut(&mut self) -> &mut T {
291348
LazyCell::force_mut(self)

0 commit comments

Comments
 (0)