Skip to content

Commit ea1cfbd

Browse files
authored
sync: reorder const_new before new_with (#6392)
1 parent fb2dc97 commit ea1cfbd

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

tokio/src/sync/once_cell.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,22 @@ impl<T> OnceCell<T> {
132132
}
133133
}
134134

135-
/// Creates a new `OnceCell` that contains the provided value, if any.
135+
/// Creates a new empty `OnceCell` instance.
136136
///
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.
138139
///
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.
153144
///
154145
/// # Example
155146
///
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-
///
161147
/// ```
162148
/// use tokio::sync::OnceCell;
163149
///
164-
/// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1);
150+
/// static ONCE: OnceCell<u32> = OnceCell::const_new();
165151
///
166152
/// async fn get_global_integer() -> &'static u32 {
167153
/// ONCE.get_or_init(|| async {
@@ -172,37 +158,51 @@ impl<T> OnceCell<T> {
172158
/// #[tokio::main]
173159
/// async fn main() {
174160
/// let result = get_global_integer().await;
175-
/// assert_eq!(*result, 1);
161+
/// assert_eq!(*result, 2);
176162
/// }
177163
/// ```
178164
///
179165
/// [`tokio-console`]: https://github.com/tokio-rs/console
180166
/// [unstable feature]: crate#unstable-features
181167
#[cfg(not(all(loom, test)))]
182-
pub const fn const_new_with(value: T) -> Self {
168+
pub const fn const_new() -> Self {
183169
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),
187173
}
188174
}
189175

190-
/// Creates a new empty `OnceCell` instance.
176+
/// Creates a new `OnceCell` that contains the provided value, if any.
191177
///
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`.
194179
///
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.
199194
///
200195
/// # Example
201196
///
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+
///
202202
/// ```
203203
/// use tokio::sync::OnceCell;
204204
///
205-
/// static ONCE: OnceCell<u32> = OnceCell::const_new();
205+
/// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1);
206206
///
207207
/// async fn get_global_integer() -> &'static u32 {
208208
/// ONCE.get_or_init(|| async {
@@ -213,18 +213,18 @@ impl<T> OnceCell<T> {
213213
/// #[tokio::main]
214214
/// async fn main() {
215215
/// let result = get_global_integer().await;
216-
/// assert_eq!(*result, 2);
216+
/// assert_eq!(*result, 1);
217217
/// }
218218
/// ```
219219
///
220220
/// [`tokio-console`]: https://github.com/tokio-rs/console
221221
/// [unstable feature]: crate#unstable-features
222222
#[cfg(not(all(loom, test)))]
223-
pub const fn const_new() -> Self {
223+
pub const fn const_new_with(value: T) -> Self {
224224
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(),
228228
}
229229
}
230230

0 commit comments

Comments
 (0)