@@ -132,36 +132,22 @@ impl<T> OnceCell<T> {
132
132
}
133
133
}
134
134
135
- /// Creates a new `OnceCell` that contains the provided value, if any .
135
+ /// Creates a new empty `OnceCell` instance .
136
136
///
137
- /// If the `Option` is `None`, this is equivalent to `OnceCell::new`.
137
+ /// Equivalent to `OnceCell::new`, except that it can be used in static
138
+ /// variables.
138
139
///
139
- /// [`OnceCell::new`]: crate::sync::OnceCell::new
140
- // Once https://github.com/rust-lang/rust/issues/73255 lands
141
- // and tokio MSRV is bumped to the rustc version with it stablised,
142
- // we can made this function available in const context,
143
- // by creating `Semaphore::const_new_closed`.
144
- pub fn new_with ( value : Option < T > ) -> Self {
145
- if let Some ( v) = value {
146
- OnceCell :: from ( v)
147
- } else {
148
- OnceCell :: new ( )
149
- }
150
- }
151
-
152
- /// Creates a new `OnceCell` that contains the provided value.
140
+ /// When using the `tracing` [unstable feature], a `OnceCell` created with
141
+ /// `const_new` will not be instrumented. As such, it will not be visible
142
+ /// in [`tokio-console`]. Instead, [`OnceCell::new`] should be used to
143
+ /// create an instrumented object if that is needed.
153
144
///
154
145
/// # Example
155
146
///
156
- /// When using the `tracing` [unstable feature], a `OnceCell` created with
157
- /// `const_new_with` will not be instrumented. As such, it will not be
158
- /// visible in [`tokio-console`]. Instead, [`OnceCell::new_with`] should be
159
- /// used to create an instrumented object if that is needed.
160
- ///
161
147
/// ```
162
148
/// use tokio::sync::OnceCell;
163
149
///
164
- /// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1 );
150
+ /// static ONCE: OnceCell<u32> = OnceCell::const_new( );
165
151
///
166
152
/// async fn get_global_integer() -> &'static u32 {
167
153
/// ONCE.get_or_init(|| async {
@@ -172,37 +158,51 @@ impl<T> OnceCell<T> {
172
158
/// #[tokio::main]
173
159
/// async fn main() {
174
160
/// let result = get_global_integer().await;
175
- /// assert_eq!(*result, 1 );
161
+ /// assert_eq!(*result, 2 );
176
162
/// }
177
163
/// ```
178
164
///
179
165
/// [`tokio-console`]: https://github.com/tokio-rs/console
180
166
/// [unstable feature]: crate#unstable-features
181
167
#[ cfg( not( all( loom, test) ) ) ]
182
- pub const fn const_new_with ( value : T ) -> Self {
168
+ pub const fn const_new ( ) -> Self {
183
169
OnceCell {
184
- value_set : AtomicBool :: new ( true ) ,
185
- value : UnsafeCell :: new ( MaybeUninit :: new ( value ) ) ,
186
- semaphore : Semaphore :: const_new_closed ( ) ,
170
+ value_set : AtomicBool :: new ( false ) ,
171
+ value : UnsafeCell :: new ( MaybeUninit :: uninit ( ) ) ,
172
+ semaphore : Semaphore :: const_new ( 1 ) ,
187
173
}
188
174
}
189
175
190
- /// Creates a new empty `OnceCell` instance .
176
+ /// Creates a new `OnceCell` that contains the provided value, if any .
191
177
///
192
- /// Equivalent to `OnceCell::new`, except that it can be used in static
193
- /// variables.
178
+ /// If the `Option` is `None`, this is equivalent to `OnceCell::new`.
194
179
///
195
- /// When using the `tracing` [unstable feature], a `OnceCell` created with
196
- /// `const_new` will not be instrumented. As such, it will not be visible
197
- /// in [`tokio-console`]. Instead, [`OnceCell::new`] should be used to
198
- /// create an instrumented object if that is needed.
180
+ /// [`OnceCell::new`]: crate::sync::OnceCell::new
181
+ // Once https://github.com/rust-lang/rust/issues/73255 lands
182
+ // and tokio MSRV is bumped to the rustc version with it stablised,
183
+ // we can made this function available in const context,
184
+ // by creating `Semaphore::const_new_closed`.
185
+ pub fn new_with ( value : Option < T > ) -> Self {
186
+ if let Some ( v) = value {
187
+ OnceCell :: from ( v)
188
+ } else {
189
+ OnceCell :: new ( )
190
+ }
191
+ }
192
+
193
+ /// Creates a new `OnceCell` that contains the provided value.
199
194
///
200
195
/// # Example
201
196
///
197
+ /// When using the `tracing` [unstable feature], a `OnceCell` created with
198
+ /// `const_new_with` will not be instrumented. As such, it will not be
199
+ /// visible in [`tokio-console`]. Instead, [`OnceCell::new_with`] should be
200
+ /// used to create an instrumented object if that is needed.
201
+ ///
202
202
/// ```
203
203
/// use tokio::sync::OnceCell;
204
204
///
205
- /// static ONCE: OnceCell<u32> = OnceCell::const_new( );
205
+ /// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1 );
206
206
///
207
207
/// async fn get_global_integer() -> &'static u32 {
208
208
/// ONCE.get_or_init(|| async {
@@ -213,18 +213,18 @@ impl<T> OnceCell<T> {
213
213
/// #[tokio::main]
214
214
/// async fn main() {
215
215
/// let result = get_global_integer().await;
216
- /// assert_eq!(*result, 2 );
216
+ /// assert_eq!(*result, 1 );
217
217
/// }
218
218
/// ```
219
219
///
220
220
/// [`tokio-console`]: https://github.com/tokio-rs/console
221
221
/// [unstable feature]: crate#unstable-features
222
222
#[ cfg( not( all( loom, test) ) ) ]
223
- pub const fn const_new ( ) -> Self {
223
+ pub const fn const_new_with ( value : T ) -> Self {
224
224
OnceCell {
225
- value_set : AtomicBool :: new ( false ) ,
226
- value : UnsafeCell :: new ( MaybeUninit :: uninit ( ) ) ,
227
- semaphore : Semaphore :: const_new ( 1 ) ,
225
+ value_set : AtomicBool :: new ( true ) ,
226
+ value : UnsafeCell :: new ( MaybeUninit :: new ( value ) ) ,
227
+ semaphore : Semaphore :: const_new_closed ( ) ,
228
228
}
229
229
}
230
230
0 commit comments