@@ -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 > {
@@ -196,12 +200,13 @@ impl<T> OnceLock<T> {
196200 unsafe { self . get_unchecked ( ) }
197201 }
198202
199- /// Sets the contents of this cell to `value`.
203+ /// Initializes the contents of the cell to `value`.
200204 ///
201205 /// 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.
203207 ///
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.
205210 ///
206211 /// # Examples
207212 ///
@@ -230,13 +235,15 @@ impl<T> OnceLock<T> {
230235 }
231236 }
232237
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.
235240 ///
236241 /// 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.
238244 ///
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.
240247 ///
241248 /// # Examples
242249 ///
@@ -269,16 +276,16 @@ impl<T> OnceLock<T> {
269276 }
270277 }
271278
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 .
274281 ///
275282 /// Many threads may call `get_or_init` concurrently with different
276283 /// initializing functions, but it is guaranteed that only one function
277284 /// will be executed.
278285 ///
279286 /// # Panics
280287 ///
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
282289 /// remains uninitialized.
283290 ///
284291 /// It is an error to reentrantly initialize the cell from `f`. The
@@ -308,13 +315,13 @@ impl<T> OnceLock<T> {
308315 }
309316
310317 /// 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 .
312319 ///
313320 /// This method never blocks.
314321 ///
315322 /// # Panics
316323 ///
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
318325 /// remains uninitialized.
319326 ///
320327 /// # Examples
@@ -345,13 +352,13 @@ impl<T> OnceLock<T> {
345352 }
346353 }
347354
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.
351358 ///
352359 /// # Panics
353360 ///
354- /// If `f` panics, the panic is propagated to the caller, and
361+ /// If `f() ` panics, the panic is propagated to the caller, and
355362 /// the cell remains uninitialized.
356363 ///
357364 /// It is an error to reentrantly initialize the cell from `f`.
@@ -397,14 +404,14 @@ impl<T> OnceLock<T> {
397404 }
398405
399406 /// 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.
402409 ///
403410 /// This method never blocks.
404411 ///
405412 /// # Panics
406413 ///
407- /// If `f` panics, the panic is propagated to the caller, and
414+ /// If `f() ` panics, the panic is propagated to the caller, and
408415 /// the cell remains uninitialized.
409416 ///
410417 /// # Examples
@@ -416,7 +423,7 @@ impl<T> OnceLock<T> {
416423 ///
417424 /// let mut cell: OnceLock<u32> = OnceLock::new();
418425 ///
419- /// // Failed initializers do not change the value
426+ /// // Failed attempts to initialize the cell do not change its contents
420427 /// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
421428 /// assert!(cell.get().is_none());
422429 ///
@@ -440,7 +447,7 @@ impl<T> OnceLock<T> {
440447 }
441448
442449 /// Consumes the `OnceLock`, returning the wrapped value. Returns
443- /// `None` if the cell was empty .
450+ /// `None` if the cell was uninitialized .
444451 ///
445452 /// # Examples
446453 ///
@@ -462,7 +469,7 @@ impl<T> OnceLock<T> {
462469
463470 /// Takes the value out of this `OnceLock`, moving it back to an uninitialized state.
464471 ///
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 .
466473 ///
467474 /// Safety is guaranteed by requiring a mutable reference.
468475 ///
@@ -528,7 +535,7 @@ impl<T> OnceLock<T> {
528535
529536 /// # Safety
530537 ///
531- /// The value must be initialized
538+ /// The cell must be initialized
532539 #[ inline]
533540 unsafe fn get_unchecked ( & self ) -> & T {
534541 debug_assert ! ( self . is_initialized( ) ) ;
@@ -537,7 +544,7 @@ impl<T> OnceLock<T> {
537544
538545 /// # Safety
539546 ///
540- /// The value must be initialized
547+ /// The cell must be initialized
541548 #[ inline]
542549 unsafe fn get_unchecked_mut ( & mut self ) -> & mut T {
543550 debug_assert ! ( self . is_initialized( ) ) ;
@@ -562,7 +569,7 @@ impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
562569
563570#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
564571impl < T > Default for OnceLock < T > {
565- /// Creates a new empty cell.
572+ /// Creates a new uninitialized cell.
566573 ///
567574 /// # Example
568575 ///
0 commit comments