Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2678593

Browse files
committedJun 15, 2024
std: suggest OnceLock over Once
1 parent c3c1757 commit 2678593

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎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.

‎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)
Failed to load comments.