@@ -15,6 +15,22 @@ enum State<T, F> {
15
15
///
16
16
/// [`std::sync::LazyLock`]: ../../std/sync/struct.LazyLock.html
17
17
///
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
+ ///
18
34
/// # Examples
19
35
///
20
36
/// ```
@@ -64,6 +80,10 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
64
80
///
65
81
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
66
82
///
83
+ /// # Panics
84
+ ///
85
+ /// Panics if the cell is poisoned.
86
+ ///
67
87
/// # Examples
68
88
///
69
89
/// ```
@@ -93,6 +113,15 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
93
113
///
94
114
/// This is equivalent to the `Deref` impl, but is explicit.
95
115
///
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
+ ///
96
125
/// # Examples
97
126
///
98
127
/// ```
@@ -123,6 +152,15 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
123
152
/// Forces the evaluation of this lazy value and returns a mutable reference to
124
153
/// the result.
125
154
///
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
+ ///
126
164
/// # Examples
127
165
///
128
166
/// ```
@@ -219,7 +257,8 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
219
257
}
220
258
221
259
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`.
223
262
///
224
263
/// # Examples
225
264
///
@@ -245,7 +284,8 @@ impl<T, F> LazyCell<T, F> {
245
284
}
246
285
}
247
286
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`.
249
289
///
250
290
/// # Examples
251
291
///
@@ -278,6 +318,15 @@ impl<T, F> LazyCell<T, F> {
278
318
#[ stable( feature = "lazy_cell" , since = "1.80.0" ) ]
279
319
impl < T , F : FnOnce ( ) -> T > Deref for LazyCell < T , F > {
280
320
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
281
330
#[ inline]
282
331
fn deref ( & self ) -> & T {
283
332
LazyCell :: force ( self )
@@ -286,6 +335,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
286
335
287
336
#[ stable( feature = "lazy_deref_mut" , since = "1.89.0" ) ]
288
337
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
289
346
#[ inline]
290
347
fn deref_mut ( & mut self ) -> & mut T {
291
348
LazyCell :: force_mut ( self )
0 commit comments