Skip to content

Commit 6412e40

Browse files
authored
Unrolled build for rust-lang#126509
Rollup merge of rust-lang#126509 - workingjubilee:gently-discourage-doing-things-once, r=jhpratt std: suggest OnceLock over Once It was noted in rust-lang#125615 (comment) that Once is not necessary in most cases and should be discouraged. Once is really just an implementation detail of OnceLock that others can use if they want. Suggest they use OnceLock instead.
2 parents 1d1356d + b8eb6ad commit 6412e40

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

library/std/src/sync/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133
//! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at
134134
//! most one thread at a time is able to access some data.
135135
//!
136-
//! - [`Once`]: Used for a thread-safe, one-time global initialization routine
136+
//! - [`Once`]: Used for a thread-safe, one-time global initialization routine.
137+
//! Mostly useful for implementing other types like `OnceLock`.
137138
//!
138139
//! - [`OnceLock`]: Used for thread-safe, one-time initialization of a
139140
//! variable, with potentially different initializers based on the caller.

library/std/src/sync/once.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ use crate::fmt;
1010
use crate::panic::{RefUnwindSafe, UnwindSafe};
1111
use crate::sys::sync as sys;
1212

13-
/// A synchronization primitive which can be used to run a one-time global
14-
/// initialization. Useful for one-time initialization for FFI or related
15-
/// functionality. This type can only be constructed with [`Once::new()`].
13+
/// A low-level synchronization primitive for one-time global execution.
14+
///
15+
/// Previously this was the only "execute once" synchronization in `std`.
16+
/// Other libraries implemented novel synchronizing types with `Once`, like
17+
/// [`OnceLock<T>`] or [`LazyLock<T, F>`], before those were added to `std`.
18+
/// `OnceLock<T>` in particular supersedes `Once` in functionality and should
19+
/// be preferred for the common case where the `Once` is associated with data.
20+
///
21+
/// This type can only be constructed with [`Once::new()`].
1622
///
1723
/// # Examples
1824
///
@@ -25,6 +31,9 @@ use crate::sys::sync as sys;
2531
/// // run initialization here
2632
/// });
2733
/// ```
34+
///
35+
/// [`OnceLock<T>`]: crate::sync::OnceLock
36+
/// [`LazyLock<T, F>`]: crate::sync::LazyLock
2837
#[stable(feature = "rust1", since = "1.0.0")]
2938
pub struct Once {
3039
inner: sys::Once,

0 commit comments

Comments
 (0)