Skip to content

Commit ee69c3a

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

File tree

2 files changed

+84
-68
lines changed

2 files changed

+84
-68
lines changed

library/core/src/cell/once.rs

+37-31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ use crate::{fmt, mem};
1010
///
1111
/// For a thread-safe version of this struct, see [`std::sync::OnceLock`].
1212
///
13+
/// A OnceCell conceptually has two states, called the `uninitialized state`
14+
/// and the `initialized state`.
15+
///
16+
/// Like an `enum { Uninitialized, Initialized(T) }`,
17+
/// except that it has invariants to uphold, so the enum is hidden.
18+
///
1319
/// [`RefCell`]: crate::cell::RefCell
1420
/// [`Cell`]: crate::cell::Cell
1521
/// [`std::sync::OnceLock`]: ../../std/sync/struct.OnceLock.html
@@ -35,7 +41,7 @@ pub struct OnceCell<T> {
3541
}
3642

3743
impl<T> OnceCell<T> {
38-
/// Creates a new empty cell.
44+
/// Creates a new cell in the uninitialized state.
3945
#[inline]
4046
#[must_use]
4147
#[stable(feature = "once_cell", since = "1.70.0")]
@@ -46,7 +52,7 @@ impl<T> OnceCell<T> {
4652

4753
/// Gets the reference to the underlying value.
4854
///
49-
/// Returns `None` if the cell is empty.
55+
/// Returns `None` if the cell is in the uninitialized state.
5056
#[inline]
5157
#[stable(feature = "once_cell", since = "1.70.0")]
5258
pub fn get(&self) -> Option<&T> {
@@ -56,19 +62,19 @@ impl<T> OnceCell<T> {
5662

5763
/// Gets the mutable reference to the underlying value.
5864
///
59-
/// Returns `None` if the cell is empty.
65+
/// Returns `None` if the cell is in the uninitialized state.
6066
#[inline]
6167
#[stable(feature = "once_cell", since = "1.70.0")]
6268
pub fn get_mut(&mut self) -> Option<&mut T> {
6369
self.inner.get_mut().as_mut()
6470
}
6571

66-
/// Sets the contents of the cell to `value`.
72+
/// Initializes the cell's value to `value`.
6773
///
6874
/// # Errors
6975
///
70-
/// This method returns `Ok(())` if the cell was empty and `Err(value)` if
71-
/// it was full.
76+
/// This method returns `Ok(())` if the cell was in the uninitialized state
77+
/// and `Err(value)` if it was already in the initialized state.
7278
///
7379
/// # Examples
7480
///
@@ -92,13 +98,13 @@ impl<T> OnceCell<T> {
9298
}
9399
}
94100

95-
/// Sets the contents of the cell to `value` if the cell was empty, then
96-
/// returns a reference to it.
101+
/// Initializes the cell's value to `value` if the cell was in the
102+
/// uninitialized state, then returns a reference to it.
97103
///
98104
/// # Errors
99105
///
100-
/// This method returns `Ok(&value)` if the cell was empty and
101-
/// `Err(&current_value, value)` if it was full.
106+
/// This method returns `Ok(&value)` if the cell was in the uninitialized state
107+
/// and `Err((&current_value, value))` if it was already in the initialized state.
102108
///
103109
/// # Examples
104110
///
@@ -130,13 +136,13 @@ impl<T> OnceCell<T> {
130136
Ok(slot.insert(value))
131137
}
132138

133-
/// Gets the contents of the cell, initializing it with `f`
134-
/// if the cell was empty.
139+
/// Gets the cell's value, initializing it to `f()`
140+
/// if the cell was in the uninitialized state.
135141
///
136142
/// # Panics
137143
///
138-
/// If `f` panics, the panic is propagated to the caller, and the cell
139-
/// remains uninitialized.
144+
/// If `f()` panics, the panic is propagated to the caller, and the cell
145+
/// remains in the uninitialized state.
140146
///
141147
/// It is an error to reentrantly initialize the cell from `f`. Doing
142148
/// so results in a panic.
@@ -163,13 +169,13 @@ impl<T> OnceCell<T> {
163169
}
164170
}
165171

166-
/// Gets the mutable reference of the contents of the cell,
167-
/// initializing it with `f` if the cell was empty.
172+
/// Gets the mutable reference of the cell's value,
173+
/// initializing it to `f()` if the cell was in the uninitialized state.
168174
///
169175
/// # Panics
170176
///
171-
/// If `f` panics, the panic is propagated to the caller, and the cell
172-
/// remains uninitialized.
177+
/// If `f()` panics, the panic is propagated to the caller, and the cell
178+
/// remains in the uninitialized state.
173179
///
174180
/// # Examples
175181
///
@@ -199,14 +205,14 @@ impl<T> OnceCell<T> {
199205
}
200206
}
201207

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.
208+
/// Gets the cell's value, initializing it to `f()` if
209+
/// the cell was in the uninitialized state. If the cell was in
210+
/// the uninitialized state and `f()` failed, an error is returned.
205211
///
206212
/// # Panics
207213
///
208-
/// If `f` panics, the panic is propagated to the caller, and the cell
209-
/// remains uninitialized.
214+
/// If `f()` panics, the panic is propagated to the caller, and the cell
215+
/// remains in the uninitialized state.
210216
///
211217
/// It is an error to reentrantly initialize the cell from `f`. Doing
212218
/// so results in a panic.
@@ -238,14 +244,14 @@ impl<T> OnceCell<T> {
238244
self.try_init(f)
239245
}
240246

241-
/// 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.
247+
/// Gets the mutable reference of the cell's value, initializing
248+
/// it to `f()` if the cell was in the uninitialized state. If the cell
249+
/// was in the uninitialized state and `f()` failed, an error is returned.
244250
///
245251
/// # Panics
246252
///
247-
/// If `f` panics, the panic is propagated to the caller, and the cell
248-
/// remains uninitialized.
253+
/// If `f()` panics, the panic is propagated to the caller, and the cell
254+
/// remains in the uninitialized state.
249255
///
250256
/// # Examples
251257
///
@@ -256,7 +262,7 @@ impl<T> OnceCell<T> {
256262
///
257263
/// let mut cell: OnceCell<u32> = OnceCell::new();
258264
///
259-
/// // Failed initializers do not change the value
265+
/// // Failed attempts to initialize cell's value has no effect on the cell
260266
/// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
261267
/// assert!(cell.get().is_none());
262268
///
@@ -295,7 +301,7 @@ impl<T> OnceCell<T> {
295301

296302
/// Consumes the cell, returning the wrapped value.
297303
///
298-
/// Returns `None` if the cell was empty.
304+
/// Returns `None` if the cell was in the uninitialized state.
299305
///
300306
/// # Examples
301307
///
@@ -321,7 +327,7 @@ impl<T> OnceCell<T> {
321327

322328
/// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
323329
///
324-
/// Has no effect and returns `None` if the `OnceCell` hasn't been initialized.
330+
/// Has no effect and returns `None` if the `OnceCell` is in the uninitialized state.
325331
///
326332
/// Safety is guaranteed by requiring a mutable reference.
327333
///

0 commit comments

Comments
 (0)