@@ -13,6 +13,9 @@ use crate::sync::Once;
13
13
/// Where OnceLock shines is when LazyLock is too simple to support a given case, as LazyLock
14
14
/// doesn't allow additional inputs to its function after you call [`LazyLock::new(|| ...)`].
15
15
///
16
+ /// A `OnceLock` can be thought of as a safe abstraction over uninitialized data that becomes
17
+ /// initialized once written.
18
+ ///
16
19
/// [`OnceCell`]: crate::cell::OnceCell
17
20
/// [`LazyLock<T, F>`]: crate::sync::LazyLock
18
21
/// [`LazyLock::new(|| ...)`]: crate::sync::LazyLock::new
@@ -126,7 +129,7 @@ pub struct OnceLock<T> {
126
129
}
127
130
128
131
impl < T > OnceLock < T > {
129
- /// Creates a new empty cell.
132
+ /// Creates a new uninitialized cell.
130
133
#[ inline]
131
134
#[ must_use]
132
135
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
@@ -141,8 +144,8 @@ impl<T> OnceLock<T> {
141
144
142
145
/// Gets the reference to the underlying value.
143
146
///
144
- /// Returns `None` if the cell is empty , or being initialized. This
145
- /// method never blocks.
147
+ /// Returns `None` if the cell is uninitialized , or being initialized.
148
+ /// This method never blocks.
146
149
#[ inline]
147
150
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
148
151
pub fn get ( & self ) -> Option < & T > {
@@ -156,7 +159,8 @@ impl<T> OnceLock<T> {
156
159
157
160
/// Gets the mutable reference to the underlying value.
158
161
///
159
- /// Returns `None` if the cell is empty. This method never blocks.
162
+ /// Returns `None` if the cell is uninitialized, or being initialized.
163
+ /// This method never blocks.
160
164
#[ inline]
161
165
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
162
166
pub fn get_mut ( & mut self ) -> Option < & mut T > {
@@ -196,12 +200,13 @@ impl<T> OnceLock<T> {
196
200
unsafe { self . get_unchecked ( ) }
197
201
}
198
202
199
- /// Sets the contents of this cell to `value`.
203
+ /// Initializes the contents of the cell to `value`.
200
204
///
201
205
/// May block if another thread is currently attempting to initialize the cell. The cell is
202
- /// guaranteed to contain a value when set returns, though not necessarily the one provided.
206
+ /// guaranteed to contain a value when ` set` returns, though not necessarily the one provided.
203
207
///
204
- /// Returns `Ok(())` if the cell's value was set by this call.
208
+ /// Returns `Ok(())` if the cell was uninitialized and
209
+ /// `Err(value)` if the cell was already initialized.
205
210
///
206
211
/// # Examples
207
212
///
@@ -230,13 +235,15 @@ impl<T> OnceLock<T> {
230
235
}
231
236
}
232
237
233
- /// Sets the contents of this cell to `value` if the cell was empty, then
234
- /// returns a reference to it.
238
+ /// Initializes the contents of the cell to `value` if the cell was uninitialized,
239
+ /// then returns a reference to it.
235
240
///
236
241
/// May block if another thread is currently attempting to initialize the cell. The cell is
237
- /// guaranteed to contain a value when set returns, though not necessarily the one provided.
242
+ /// guaranteed to contain a value when `try_insert` returns, though not necessarily the
243
+ /// one provided.
238
244
///
239
- /// Returns `Ok(&value)` if the cell was empty and `Err(¤t_value, value)` if it was full.
245
+ /// Returns `Ok(&value)` if the cell was uninitialized and
246
+ /// `Err((¤t_value, value))` if it was already initialized.
240
247
///
241
248
/// # Examples
242
249
///
@@ -269,16 +276,16 @@ impl<T> OnceLock<T> {
269
276
}
270
277
}
271
278
272
- /// Gets the contents of the cell, initializing it with `f` if the cell
273
- /// was empty .
279
+ /// Gets the contents of the cell, initializing it to `f() ` if the cell
280
+ /// was uninitialized .
274
281
///
275
282
/// Many threads may call `get_or_init` concurrently with different
276
283
/// initializing functions, but it is guaranteed that only one function
277
284
/// will be executed.
278
285
///
279
286
/// # Panics
280
287
///
281
- /// If `f` panics, the panic is propagated to the caller, and the cell
288
+ /// If `f() ` panics, the panic is propagated to the caller, and the cell
282
289
/// remains uninitialized.
283
290
///
284
291
/// It is an error to reentrantly initialize the cell from `f`. The
@@ -308,13 +315,13 @@ impl<T> OnceLock<T> {
308
315
}
309
316
310
317
/// Gets the mutable reference of the contents of the cell, initializing
311
- /// it with `f` if the cell was empty .
318
+ /// it to `f() ` if the cell was uninitialized .
312
319
///
313
320
/// This method never blocks.
314
321
///
315
322
/// # Panics
316
323
///
317
- /// If `f` panics, the panic is propagated to the caller, and the cell
324
+ /// If `f() ` panics, the panic is propagated to the caller, and the cell
318
325
/// remains uninitialized.
319
326
///
320
327
/// # Examples
@@ -345,13 +352,13 @@ impl<T> OnceLock<T> {
345
352
}
346
353
}
347
354
348
- /// Gets the contents of the cell, initializing it with `f` if
349
- /// the cell was empty . If the cell was empty and `f` failed, an
350
- /// error is returned.
355
+ /// Gets the contents of the cell, initializing it to `f() ` if
356
+ /// the cell was uninitialized . If the cell was uninitialized
357
+ /// and `f()` failed, an error is returned.
351
358
///
352
359
/// # Panics
353
360
///
354
- /// If `f` panics, the panic is propagated to the caller, and
361
+ /// If `f() ` panics, the panic is propagated to the caller, and
355
362
/// the cell remains uninitialized.
356
363
///
357
364
/// It is an error to reentrantly initialize the cell from `f`.
@@ -397,14 +404,14 @@ impl<T> OnceLock<T> {
397
404
}
398
405
399
406
/// Gets the mutable reference of the contents of the cell, initializing
400
- /// it with `f` if the cell was empty . If the cell was empty and `f` failed,
401
- /// an error is returned.
407
+ /// it to `f() ` if the cell was uninitialized . If the cell was uninitialized
408
+ /// and `f()` failed, an error is returned.
402
409
///
403
410
/// This method never blocks.
404
411
///
405
412
/// # Panics
406
413
///
407
- /// If `f` panics, the panic is propagated to the caller, and
414
+ /// If `f() ` panics, the panic is propagated to the caller, and
408
415
/// the cell remains uninitialized.
409
416
///
410
417
/// # Examples
@@ -416,7 +423,7 @@ impl<T> OnceLock<T> {
416
423
///
417
424
/// let mut cell: OnceLock<u32> = OnceLock::new();
418
425
///
419
- /// // Failed initializers do not change the value
426
+ /// // Failed attempts to initialize the cell do not change its contents
420
427
/// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
421
428
/// assert!(cell.get().is_none());
422
429
///
@@ -440,7 +447,7 @@ impl<T> OnceLock<T> {
440
447
}
441
448
442
449
/// Consumes the `OnceLock`, returning the wrapped value. Returns
443
- /// `None` if the cell was empty .
450
+ /// `None` if the cell was uninitialized .
444
451
///
445
452
/// # Examples
446
453
///
@@ -462,7 +469,7 @@ impl<T> OnceLock<T> {
462
469
463
470
/// Takes the value out of this `OnceLock`, moving it back to an uninitialized state.
464
471
///
465
- /// Has no effect and returns `None` if the `OnceLock` hasn't been initialized .
472
+ /// Has no effect and returns `None` if the `OnceLock` was uninitialized .
466
473
///
467
474
/// Safety is guaranteed by requiring a mutable reference.
468
475
///
@@ -528,7 +535,7 @@ impl<T> OnceLock<T> {
528
535
529
536
/// # Safety
530
537
///
531
- /// The value must be initialized
538
+ /// The cell must be initialized
532
539
#[ inline]
533
540
unsafe fn get_unchecked ( & self ) -> & T {
534
541
debug_assert ! ( self . is_initialized( ) ) ;
@@ -537,7 +544,7 @@ impl<T> OnceLock<T> {
537
544
538
545
/// # Safety
539
546
///
540
- /// The value must be initialized
547
+ /// The cell must be initialized
541
548
#[ inline]
542
549
unsafe fn get_unchecked_mut ( & mut self ) -> & mut T {
543
550
debug_assert ! ( self . is_initialized( ) ) ;
@@ -562,7 +569,7 @@ impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
562
569
563
570
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
564
571
impl < T > Default for OnceLock < T > {
565
- /// Creates a new empty cell.
572
+ /// Creates a new uninitialized cell.
566
573
///
567
574
/// # Example
568
575
///
0 commit comments