@@ -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 > {
@@ -194,12 +198,13 @@ impl<T> OnceLock<T> {
194
198
unsafe { self . get_unchecked ( ) }
195
199
}
196
200
197
- /// Sets the contents of this cell to `value`.
201
+ /// Initializes the contents of the cell to `value`.
198
202
///
199
203
/// May block if another thread is currently attempting to initialize the cell. The cell is
200
- /// guaranteed to contain a value when set returns, though not necessarily the one provided.
204
+ /// guaranteed to contain a value when ` set` returns, though not necessarily the one provided.
201
205
///
202
- /// Returns `Ok(())` if the cell's value was set by this call.
206
+ /// Returns `Ok(())` if the cell was uninitialized and
207
+ /// `Err(value)` if the cell was already initialized.
203
208
///
204
209
/// # Examples
205
210
///
@@ -228,13 +233,15 @@ impl<T> OnceLock<T> {
228
233
}
229
234
}
230
235
231
- /// Sets the contents of this cell to `value` if the cell was empty, then
232
- /// returns a reference to it.
236
+ /// Initializes the contents of the cell to `value` if the cell was uninitialized,
237
+ /// then returns a reference to it.
233
238
///
234
239
/// May block if another thread is currently attempting to initialize the cell. The cell is
235
- /// guaranteed to contain a value when set returns, though not necessarily the one provided.
240
+ /// guaranteed to contain a value when `try_insert` returns, though not necessarily the
241
+ /// one provided.
236
242
///
237
- /// Returns `Ok(&value)` if the cell was empty and `Err(¤t_value, value)` if it was full.
243
+ /// Returns `Ok(&value)` if the cell was uninitialized and
244
+ /// `Err((¤t_value, value))` if it was already initialized.
238
245
///
239
246
/// # Examples
240
247
///
@@ -267,16 +274,16 @@ impl<T> OnceLock<T> {
267
274
}
268
275
}
269
276
270
- /// Gets the contents of the cell, initializing it with `f` if the cell
271
- /// was empty .
277
+ /// Gets the contents of the cell, initializing it to `f() ` if the cell
278
+ /// was uninitialized .
272
279
///
273
280
/// Many threads may call `get_or_init` concurrently with different
274
281
/// initializing functions, but it is guaranteed that only one function
275
282
/// will be executed.
276
283
///
277
284
/// # Panics
278
285
///
279
- /// If `f` panics, the panic is propagated to the caller, and the cell
286
+ /// If `f() ` panics, the panic is propagated to the caller, and the cell
280
287
/// remains uninitialized.
281
288
///
282
289
/// It is an error to reentrantly initialize the cell from `f`. The
@@ -306,13 +313,13 @@ impl<T> OnceLock<T> {
306
313
}
307
314
308
315
/// Gets the mutable reference of the contents of the cell, initializing
309
- /// it with `f` if the cell was empty .
316
+ /// it to `f() ` if the cell was uninitialized .
310
317
///
311
318
/// This method never blocks.
312
319
///
313
320
/// # Panics
314
321
///
315
- /// If `f` panics, the panic is propagated to the caller, and the cell
322
+ /// If `f() ` panics, the panic is propagated to the caller, and the cell
316
323
/// remains uninitialized.
317
324
///
318
325
/// # Examples
@@ -343,13 +350,13 @@ impl<T> OnceLock<T> {
343
350
}
344
351
}
345
352
346
- /// Gets the contents of the cell, initializing it with `f` if
347
- /// the cell was empty . If the cell was empty and `f` failed, an
348
- /// error is returned.
353
+ /// Gets the contents of the cell, initializing it to `f() ` if
354
+ /// the cell was uninitialized . If the cell was uninitialized
355
+ /// and `f()` failed, an error is returned.
349
356
///
350
357
/// # Panics
351
358
///
352
- /// If `f` panics, the panic is propagated to the caller, and
359
+ /// If `f() ` panics, the panic is propagated to the caller, and
353
360
/// the cell remains uninitialized.
354
361
///
355
362
/// It is an error to reentrantly initialize the cell from `f`.
@@ -395,14 +402,14 @@ impl<T> OnceLock<T> {
395
402
}
396
403
397
404
/// Gets the mutable reference of the contents of the cell, initializing
398
- /// it with `f` if the cell was empty . If the cell was empty and `f` failed,
399
- /// an error is returned.
405
+ /// it to `f() ` if the cell was uninitialized . If the cell was uninitialized
406
+ /// and `f()` failed, an error is returned.
400
407
///
401
408
/// This method never blocks.
402
409
///
403
410
/// # Panics
404
411
///
405
- /// If `f` panics, the panic is propagated to the caller, and
412
+ /// If `f() ` panics, the panic is propagated to the caller, and
406
413
/// the cell remains uninitialized.
407
414
///
408
415
/// # Examples
@@ -414,7 +421,7 @@ impl<T> OnceLock<T> {
414
421
///
415
422
/// let mut cell: OnceLock<u32> = OnceLock::new();
416
423
///
417
- /// // Failed initializers do not change the value
424
+ /// // Failed attempts to initialize the cell do not change its contents
418
425
/// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
419
426
/// assert!(cell.get().is_none());
420
427
///
@@ -438,7 +445,7 @@ impl<T> OnceLock<T> {
438
445
}
439
446
440
447
/// Consumes the `OnceLock`, returning the wrapped value. Returns
441
- /// `None` if the cell was empty .
448
+ /// `None` if the cell was uninitialized .
442
449
///
443
450
/// # Examples
444
451
///
@@ -460,7 +467,7 @@ impl<T> OnceLock<T> {
460
467
461
468
/// Takes the value out of this `OnceLock`, moving it back to an uninitialized state.
462
469
///
463
- /// Has no effect and returns `None` if the `OnceLock` hasn't been initialized .
470
+ /// Has no effect and returns `None` if the `OnceLock` was uninitialized .
464
471
///
465
472
/// Safety is guaranteed by requiring a mutable reference.
466
473
///
@@ -526,7 +533,7 @@ impl<T> OnceLock<T> {
526
533
527
534
/// # Safety
528
535
///
529
- /// The value must be initialized
536
+ /// The cell must be initialized
530
537
#[ inline]
531
538
unsafe fn get_unchecked ( & self ) -> & T {
532
539
debug_assert ! ( self . is_initialized( ) ) ;
@@ -535,7 +542,7 @@ impl<T> OnceLock<T> {
535
542
536
543
/// # Safety
537
544
///
538
- /// The value must be initialized
545
+ /// The cell must be initialized
539
546
#[ inline]
540
547
unsafe fn get_unchecked_mut ( & mut self ) -> & mut T {
541
548
debug_assert ! ( self . is_initialized( ) ) ;
@@ -560,7 +567,7 @@ impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
560
567
561
568
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
562
569
impl < T > Default for OnceLock < T > {
563
- /// Creates a new empty cell.
570
+ /// Creates a new uninitialized cell.
564
571
///
565
572
/// # Example
566
573
///
0 commit comments