@@ -25,6 +25,22 @@ union Data<T, F> {
25
25
///
26
26
/// [`LazyCell`]: crate::cell::LazyCell
27
27
///
28
+ /// # Poisoning
29
+ ///
30
+ /// If the initialization closure passed to [`LazyLock::new`] panics, the lock will be poisoned.
31
+ /// Once the lock is poisoned, any threads that attempt to access this lock (via a dereference
32
+ /// or via an explicit call to [`force()`]) will panic.
33
+ ///
34
+ /// This concept is similar to that of poisoning in the [`std::sync::poison`] module. A key
35
+ /// difference, however, is that poisoning in `LazyLock` is _unrecoverable_. All future accesses of
36
+ /// the lock from other threads will panic, whereas a type in [`std::sync::poison`] like
37
+ /// [`std::sync::poison::Mutex`] allows recovery via [`PoisonError::into_inner()`].
38
+ ///
39
+ /// [`force()`]: LazyLock::force
40
+ /// [`std::sync::poison`]: crate::sync::poison
41
+ /// [`std::sync::poison::Mutex`]: crate::sync::poison::Mutex
42
+ /// [`PoisonError::into_inner()`]: crate::sync::poison::PoisonError::into_inner
43
+ ///
28
44
/// # Examples
29
45
///
30
46
/// Initialize static variables with `LazyLock`.
@@ -102,6 +118,10 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
102
118
///
103
119
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
104
120
///
121
+ /// # Panics
122
+ ///
123
+ /// Panics if the lock is poisoned.
124
+ ///
105
125
/// # Examples
106
126
///
107
127
/// ```
@@ -136,6 +156,15 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
136
156
/// Forces the evaluation of this lazy value and returns a mutable reference to
137
157
/// the result.
138
158
///
159
+ /// # Panics
160
+ ///
161
+ /// If the initialization closure panics (the one that is passed to the [`new()`] method), the
162
+ /// panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
163
+ /// accesses of the lock (via [`force()`] or a dereference) to panic.
164
+ ///
165
+ /// [`new()`]: LazyLock::new
166
+ /// [`force()`]: LazyLock::force
167
+ ///
139
168
/// # Examples
140
169
///
141
170
/// ```
@@ -193,6 +222,15 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
193
222
/// This method will block the calling thread if another initialization
194
223
/// routine is currently running.
195
224
///
225
+ /// # Panics
226
+ ///
227
+ /// If the initialization closure panics (the one that is passed to the [`new()`] method), the
228
+ /// panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
229
+ /// accesses of the lock (via [`force()`] or a dereference) to panic.
230
+ ///
231
+ /// [`new()`]: LazyLock::new
232
+ /// [`force()`]: LazyLock::force
233
+ ///
196
234
/// # Examples
197
235
///
198
236
/// ```
@@ -227,7 +265,8 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
227
265
}
228
266
229
267
impl < T , F > LazyLock < T , F > {
230
- /// Returns a mutable reference to the value if initialized, or `None` if not.
268
+ /// Returns a mutable reference to the value if initialized. Otherwise (if uninitialized or
269
+ /// poisoned), returns `None`.
231
270
///
232
271
/// # Examples
233
272
///
@@ -256,7 +295,8 @@ impl<T, F> LazyLock<T, F> {
256
295
}
257
296
}
258
297
259
- /// Returns a reference to the value if initialized, or `None` if not.
298
+ /// Returns a reference to the value if initialized. Otherwise (if uninitialized or poisoned),
299
+ /// returns `None`.
260
300
///
261
301
/// # Examples
262
302
///
@@ -307,6 +347,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
307
347
/// This method will block the calling thread if another initialization
308
348
/// routine is currently running.
309
349
///
350
+ /// # Panics
351
+ ///
352
+ /// If the initialization closure panics (the one that is passed to the [`new()`] method), the
353
+ /// panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
354
+ /// accesses of the lock (via [`force()`] or a dereference) to panic.
355
+ ///
356
+ /// [`new()`]: LazyLock::new
357
+ /// [`force()`]: LazyLock::force
310
358
#[ inline]
311
359
fn deref ( & self ) -> & T {
312
360
LazyLock :: force ( self )
@@ -315,6 +363,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
315
363
316
364
#[ stable( feature = "lazy_deref_mut" , since = "1.89.0" ) ]
317
365
impl < T , F : FnOnce ( ) -> T > DerefMut for LazyLock < T , F > {
366
+ /// # Panics
367
+ ///
368
+ /// If the initialization closure panics (the one that is passed to the [`new()`] method), the
369
+ /// panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
370
+ /// accesses of the lock (via [`force()`] or a dereference) to panic.
371
+ ///
372
+ /// [`new()`]: LazyLock::new
373
+ /// [`force()`]: LazyLock::force
318
374
#[ inline]
319
375
fn deref_mut ( & mut self ) -> & mut T {
320
376
LazyLock :: force_mut ( self )
0 commit comments