@@ -13,6 +13,9 @@ use crate::sync::Once;
1313/// Where OnceLock shines is when LazyLock is too simple to support a given case, as LazyLock
1414/// doesn't allow additional inputs to its function after you call [`LazyLock::new(|| ...)`].
1515///
16+ /// A `OnceLock` can be thought of as a safe abstraction over uninitialized data that becomes
17+ /// initialized once written.
18+ ///
1619/// [`OnceCell`]: crate::cell::OnceCell
1720/// [`LazyLock<T, F>`]: crate::sync::LazyLock
1821/// [`LazyLock::new(|| ...)`]: crate::sync::LazyLock::new
@@ -126,7 +129,7 @@ pub struct OnceLock<T> {
126129}
127130
128131impl < T > OnceLock < T > {
129- /// Creates a new empty cell.
132+ /// Creates a new uninitialized cell.
130133 #[ inline]
131134 #[ must_use]
132135 #[ stable( feature = "once_cell" , since = "1.70.0" ) ]
@@ -141,8 +144,8 @@ impl<T> OnceLock<T> {
141144
142145 /// Gets the reference to the underlying value.
143146 ///
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.
146149 #[ inline]
147150 #[ stable( feature = "once_cell" , since = "1.70.0" ) ]
148151 pub fn get ( & self ) -> Option < & T > {
@@ -156,7 +159,8 @@ impl<T> OnceLock<T> {
156159
157160 /// Gets the mutable reference to the underlying value.
158161 ///
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.
160164 #[ inline]
161165 #[ stable( feature = "once_cell" , since = "1.70.0" ) ]
162166 pub fn get_mut ( & mut self ) -> Option < & mut T > {
@@ -194,12 +198,13 @@ impl<T> OnceLock<T> {
194198 unsafe { self . get_unchecked ( ) }
195199 }
196200
197- /// Sets the contents of this cell to `value`.
201+ /// Initializes the contents of the cell to `value`.
198202 ///
199203 /// 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.
201205 ///
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.
203208 ///
204209 /// # Examples
205210 ///
@@ -228,13 +233,15 @@ impl<T> OnceLock<T> {
228233 }
229234 }
230235
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.
233238 ///
234239 /// 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.
236242 ///
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.
238245 ///
239246 /// # Examples
240247 ///
@@ -267,16 +274,16 @@ impl<T> OnceLock<T> {
267274 }
268275 }
269276
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 .
272279 ///
273280 /// Many threads may call `get_or_init` concurrently with different
274281 /// initializing functions, but it is guaranteed that only one function
275282 /// will be executed.
276283 ///
277284 /// # Panics
278285 ///
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
280287 /// remains uninitialized.
281288 ///
282289 /// It is an error to reentrantly initialize the cell from `f`. The
@@ -306,13 +313,13 @@ impl<T> OnceLock<T> {
306313 }
307314
308315 /// 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 .
310317 ///
311318 /// This method never blocks.
312319 ///
313320 /// # Panics
314321 ///
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
316323 /// remains uninitialized.
317324 ///
318325 /// # Examples
@@ -343,13 +350,13 @@ impl<T> OnceLock<T> {
343350 }
344351 }
345352
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.
349356 ///
350357 /// # Panics
351358 ///
352- /// If `f` panics, the panic is propagated to the caller, and
359+ /// If `f() ` panics, the panic is propagated to the caller, and
353360 /// the cell remains uninitialized.
354361 ///
355362 /// It is an error to reentrantly initialize the cell from `f`.
@@ -395,14 +402,14 @@ impl<T> OnceLock<T> {
395402 }
396403
397404 /// 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.
400407 ///
401408 /// This method never blocks.
402409 ///
403410 /// # Panics
404411 ///
405- /// If `f` panics, the panic is propagated to the caller, and
412+ /// If `f() ` panics, the panic is propagated to the caller, and
406413 /// the cell remains uninitialized.
407414 ///
408415 /// # Examples
@@ -414,7 +421,7 @@ impl<T> OnceLock<T> {
414421 ///
415422 /// let mut cell: OnceLock<u32> = OnceLock::new();
416423 ///
417- /// // Failed initializers do not change the value
424+ /// // Failed attempts to initialize the cell do not change its contents
418425 /// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
419426 /// assert!(cell.get().is_none());
420427 ///
@@ -438,7 +445,7 @@ impl<T> OnceLock<T> {
438445 }
439446
440447 /// Consumes the `OnceLock`, returning the wrapped value. Returns
441- /// `None` if the cell was empty .
448+ /// `None` if the cell was uninitialized .
442449 ///
443450 /// # Examples
444451 ///
@@ -460,7 +467,7 @@ impl<T> OnceLock<T> {
460467
461468 /// Takes the value out of this `OnceLock`, moving it back to an uninitialized state.
462469 ///
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 .
464471 ///
465472 /// Safety is guaranteed by requiring a mutable reference.
466473 ///
@@ -526,7 +533,7 @@ impl<T> OnceLock<T> {
526533
527534 /// # Safety
528535 ///
529- /// The value must be initialized
536+ /// The cell must be initialized
530537 #[ inline]
531538 unsafe fn get_unchecked ( & self ) -> & T {
532539 debug_assert ! ( self . is_initialized( ) ) ;
@@ -535,7 +542,7 @@ impl<T> OnceLock<T> {
535542
536543 /// # Safety
537544 ///
538- /// The value must be initialized
545+ /// The cell must be initialized
539546 #[ inline]
540547 unsafe fn get_unchecked_mut ( & mut self ) -> & mut T {
541548 debug_assert ! ( self . is_initialized( ) ) ;
@@ -560,7 +567,7 @@ impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
560567
561568#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
562569impl < T > Default for OnceLock < T > {
563- /// Creates a new empty cell.
570+ /// Creates a new uninitialized cell.
564571 ///
565572 /// # Example
566573 ///
0 commit comments