Skip to content

Commit 3b009c8

Browse files
authored
Rollup merge of rust-lang#136289 - Pyr0de:oncecell-docs, r=tgross35
OnceCell & OnceLock docs: Using (un)initialized consistently Changed * `set` / `initialize` / `full` to `initialized state` * `uninitialize` / `empty` to `uninitialized state` * `f` to `f()` * Added explaination of `uninitialized state` & `initialized state` [OnceCell Docs](https://doc.rust-lang.org/nightly/std/cell/struct.OnceCell.html) [OnceLock Docs](https://doc.rust-lang.org/nightly/std/sync/struct.OnceLock.html) Fixes rust-lang#85716 `@rustbot` label +A-docs
2 parents 0fd4bf1 + f8b01b3 commit 3b009c8

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> {
@@ -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(&current_value, value)` if it was full.
243+
/// Returns `Ok(&value)` if the cell was uninitialized and
244+
/// `Err((&current_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")]
562569
impl<T> Default for OnceLock<T> {
563-
/// Creates a new empty cell.
570+
/// Creates a new uninitialized cell.
564571
///
565572
/// # Example
566573
///

0 commit comments

Comments
 (0)