@@ -10,6 +10,12 @@ use crate::{fmt, mem};
10
10
///
11
11
/// For a thread-safe version of this struct, see [`std::sync::OnceLock`].
12
12
///
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
+ ///
13
19
/// [`RefCell`]: crate::cell::RefCell
14
20
/// [`Cell`]: crate::cell::Cell
15
21
/// [`std::sync::OnceLock`]: ../../std/sync/struct.OnceLock.html
@@ -35,7 +41,7 @@ pub struct OnceCell<T> {
35
41
}
36
42
37
43
impl < T > OnceCell < T > {
38
- /// Creates a new empty cell.
44
+ /// Creates a new cell in the uninitialized state .
39
45
#[ inline]
40
46
#[ must_use]
41
47
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
@@ -46,7 +52,7 @@ impl<T> OnceCell<T> {
46
52
47
53
/// Gets the reference to the underlying value.
48
54
///
49
- /// Returns `None` if the cell is empty .
55
+ /// Returns `None` if the cell is in the uninitialized state .
50
56
#[ inline]
51
57
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
52
58
pub fn get ( & self ) -> Option < & T > {
@@ -56,19 +62,19 @@ impl<T> OnceCell<T> {
56
62
57
63
/// Gets the mutable reference to the underlying value.
58
64
///
59
- /// Returns `None` if the cell is empty .
65
+ /// Returns `None` if the cell is in the uninitialized state .
60
66
#[ inline]
61
67
#[ stable( feature = "once_cell" , since = "1.70.0" ) ]
62
68
pub fn get_mut ( & mut self ) -> Option < & mut T > {
63
69
self . inner . get_mut ( ) . as_mut ( )
64
70
}
65
71
66
- /// Sets the contents of the cell to `value`.
72
+ /// Initializes the cell's value to `value`.
67
73
///
68
74
/// # Errors
69
75
///
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 .
72
78
///
73
79
/// # Examples
74
80
///
@@ -92,13 +98,13 @@ impl<T> OnceCell<T> {
92
98
}
93
99
}
94
100
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.
97
103
///
98
104
/// # Errors
99
105
///
100
- /// This method returns `Ok(&value)` if the cell was empty and
101
- /// `Err(¤t_value, value)` if it was full .
106
+ /// This method returns `Ok(&value)` if the cell was in the uninitialized state
107
+ /// and `Err(( ¤t_value, value)) ` if it was already in the initialized state .
102
108
///
103
109
/// # Examples
104
110
///
@@ -130,13 +136,13 @@ impl<T> OnceCell<T> {
130
136
Ok ( slot. insert ( value) )
131
137
}
132
138
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 .
135
141
///
136
142
/// # Panics
137
143
///
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 .
140
146
///
141
147
/// It is an error to reentrantly initialize the cell from `f`. Doing
142
148
/// so results in a panic.
@@ -163,13 +169,13 @@ impl<T> OnceCell<T> {
163
169
}
164
170
}
165
171
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 .
168
174
///
169
175
/// # Panics
170
176
///
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 .
173
179
///
174
180
/// # Examples
175
181
///
@@ -199,14 +205,14 @@ impl<T> OnceCell<T> {
199
205
}
200
206
}
201
207
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.
205
211
///
206
212
/// # Panics
207
213
///
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 .
210
216
///
211
217
/// It is an error to reentrantly initialize the cell from `f`. Doing
212
218
/// so results in a panic.
@@ -238,14 +244,14 @@ impl<T> OnceCell<T> {
238
244
self . try_init ( f)
239
245
}
240
246
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.
244
250
///
245
251
/// # Panics
246
252
///
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 .
249
255
///
250
256
/// # Examples
251
257
///
@@ -256,7 +262,7 @@ impl<T> OnceCell<T> {
256
262
///
257
263
/// let mut cell: OnceCell<u32> = OnceCell::new();
258
264
///
259
- /// // Failed initializers do not change the value
265
+ /// // Failed attempts to initialize cell's value has no effect on the cell
260
266
/// assert!(cell.get_mut_or_try_init(|| "not a number!".parse()).is_err());
261
267
/// assert!(cell.get().is_none());
262
268
///
@@ -295,7 +301,7 @@ impl<T> OnceCell<T> {
295
301
296
302
/// Consumes the cell, returning the wrapped value.
297
303
///
298
- /// Returns `None` if the cell was empty .
304
+ /// Returns `None` if the cell was in the uninitialized state .
299
305
///
300
306
/// # Examples
301
307
///
@@ -321,7 +327,7 @@ impl<T> OnceCell<T> {
321
327
322
328
/// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
323
329
///
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 .
325
331
///
326
332
/// Safety is guaranteed by requiring a mutable reference.
327
333
///
0 commit comments