Skip to content

Commit f8b01b3

Browse files
committed
OnceCell & OnceLock docs: Using (un)initialized consistently
1 parent 5e55679 commit f8b01b3

File tree

2 files changed

+64
-54
lines changed

2 files changed

+64
-54
lines changed

library/core/src/cell/once.rs

+28-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::{fmt, mem};
88
/// only immutable references can be obtained unless one has a mutable reference to the cell
99
/// itself. In the same vein, the cell can only be re-initialized with such a mutable reference.
1010
///
11+
/// A `OnceCell` can be thought of as a safe abstraction over uninitialized data that becomes
12+
/// initialized once written.
13+
///
1114
/// For a thread-safe version of this struct, see [`std::sync::OnceLock`].
1215
///
1316
/// [`RefCell`]: crate::cell::RefCell
@@ -35,7 +38,7 @@ pub struct OnceCell<T> {
3538
}
3639

3740
impl<T> OnceCell<T> {
38-
/// Creates a new empty cell.
41+
/// Creates a new uninitialized cell.
3942
#[inline]
4043
#[must_use]
4144
#[stable(feature = "once_cell", since = "1.70.0")]
@@ -46,7 +49,7 @@ impl<T> OnceCell<T> {
4649

4750
/// Gets the reference to the underlying value.
4851
///
49-
/// Returns `None` if the cell is empty.
52+
/// Returns `None` if the cell is uninitialized.
5053
#[inline]
5154
#[stable(feature = "once_cell", since = "1.70.0")]
5255
pub fn get(&self) -> Option<&T> {
@@ -56,19 +59,19 @@ impl<T> OnceCell<T> {
5659

5760
/// Gets the mutable reference to the underlying value.
5861
///
59-
/// Returns `None` if the cell is empty.
62+
/// Returns `None` if the cell is uninitialized.
6063
#[inline]
6164
#[stable(feature = "once_cell", since = "1.70.0")]
6265
pub fn get_mut(&mut self) -> Option<&mut T> {
6366
self.inner.get_mut().as_mut()
6467
}
6568

66-
/// Sets the contents of the cell to `value`.
69+
/// Initializes the contents of the cell to `value`.
6770
///
6871
/// # Errors
6972
///
70-
/// This method returns `Ok(())` if the cell was empty and `Err(value)` if
71-
/// it was full.
73+
/// This method returns `Ok(())` if the cell was uninitialized
74+
/// and `Err(value)` if it was already initialized.
7275
///
7376
/// # Examples
7477
///
@@ -92,13 +95,13 @@ impl<T> OnceCell<T> {
9295
}
9396
}
9497

95-
/// Sets the contents of the cell to `value` if the cell was empty, then
96-
/// returns a reference to it.
98+
/// Initializes the contents of the cell to `value` if the cell was
99+
/// uninitialized, then returns a reference to it.
97100
///
98101
/// # Errors
99102
///
100-
/// This method returns `Ok(&value)` if the cell was empty and
101-
/// `Err(&current_value, value)` if it was full.
103+
/// This method returns `Ok(&value)` if the cell was uninitialized
104+
/// and `Err((&current_value, value))` if it was already initialized.
102105
///
103106
/// # Examples
104107
///
@@ -130,12 +133,12 @@ impl<T> OnceCell<T> {
130133
Ok(slot.insert(value))
131134
}
132135

133-
/// Gets the contents of the cell, initializing it with `f`
134-
/// if the cell was empty.
136+
/// Gets the contents of the cell, initializing it to `f()`
137+
/// if the cell was uninitialized.
135138
///
136139
/// # Panics
137140
///
138-
/// If `f` panics, the panic is propagated to the caller, and the cell
141+
/// If `f()` panics, the panic is propagated to the caller, and the cell
139142
/// remains uninitialized.
140143
///
141144
/// It is an error to reentrantly initialize the cell from `f`. Doing
@@ -164,11 +167,11 @@ impl<T> OnceCell<T> {
164167
}
165168

166169
/// Gets the mutable reference of the contents of the cell,
167-
/// initializing it with `f` if the cell was empty.
170+
/// initializing it to `f()` if the cell was uninitialized.
168171
///
169172
/// # Panics
170173
///
171-
/// If `f` panics, the panic is propagated to the caller, and the cell
174+
/// If `f()` panics, the panic is propagated to the caller, and the cell
172175
/// remains uninitialized.
173176
///
174177
/// # Examples
@@ -199,13 +202,13 @@ impl<T> OnceCell<T> {
199202
}
200203
}
201204

202-
/// Gets the contents of the cell, initializing it with `f` if
203-
/// the cell was empty. If the cell was empty and `f` failed, an
204-
/// error is returned.
205+
/// Gets the contents of the cell, initializing it to `f()` if
206+
/// the cell was uninitialized. If the cell was uninitialized
207+
/// and `f()` failed, an error is returned.
205208
///
206209
/// # Panics
207210
///
208-
/// If `f` panics, the panic is propagated to the caller, and the cell
211+
/// If `f()` panics, the panic is propagated to the caller, and the cell
209212
/// remains uninitialized.
210213
///
211214
/// It is an error to reentrantly initialize the cell from `f`. Doing
@@ -239,12 +242,12 @@ impl<T> OnceCell<T> {
239242
}
240243

241244
/// Gets the mutable reference of the contents of the cell, initializing
242-
/// it with `f` if the cell was empty. If the cell was empty and `f` failed,
243-
/// an error is returned.
245+
/// it to `f()` if the cell was uninitialized. If the cell was uninitialized
246+
/// and `f()` failed, an error is returned.
244247
///
245248
/// # Panics
246249
///
247-
/// If `f` panics, the panic is propagated to the caller, and the cell
250+
/// If `f()` panics, the panic is propagated to the caller, and the cell
248251
/// remains uninitialized.
249252
///
250253
/// # Examples
@@ -256,7 +259,7 @@ impl<T> OnceCell<T> {
256259
///
257260
/// let mut cell: OnceCell<u32> = OnceCell::new();
258261
///
259-
/// // Failed initializers do not change the value
262+
/// // Failed attempts to initialize the cell do not change its contents
260263
/// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
261264
/// assert!(cell.get().is_none());
262265
///
@@ -295,7 +298,7 @@ impl<T> OnceCell<T> {
295298

296299
/// Consumes the cell, returning the wrapped value.
297300
///
298-
/// Returns `None` if the cell was empty.
301+
/// Returns `None` if the cell was uninitialized.
299302
///
300303
/// # Examples
301304
///
@@ -321,7 +324,7 @@ impl<T> OnceCell<T> {
321324

322325
/// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
323326
///
324-
/// Has no effect and returns `None` if the `OnceCell` hasn't been initialized.
327+
/// Has no effect and returns `None` if the `OnceCell` is uninitialized.
325328
///
326329
/// Safety is guaranteed by requiring a mutable reference.
327330
///

library/std/src/sync/once_lock.rs

+36-29
Original file line numberDiff line numberDiff line change
@@ -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

128131
impl<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(&current_value, value)` if it was full.
245+
/// Returns `Ok(&value)` if the cell was uninitialized and
246+
/// `Err((&current_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")]
564571
impl<T> Default for OnceLock<T> {
565-
/// Creates a new empty cell.
572+
/// Creates a new uninitialized cell.
566573
///
567574
/// # Example
568575
///

0 commit comments

Comments
 (0)