diff options
| author | Jubilee Young <workingjubilee@gmail.com> | 2024-06-14 22:38:52 -0700 |
|---|---|---|
| committer | Jubilee Young <workingjubilee@gmail.com> | 2024-06-15 00:09:03 -0700 |
| commit | b8eb6ad03273d42d6a22ced57a46e5652fb35f1d (patch) | |
| tree | 4a83d1520e49f76f0d97757a8235650dc9c61926 | |
| parent | 3fc81daffd18aa1fc00a1d67cfaca12a32a07e46 (diff) | |
| download | rust-b8eb6ad03273d42d6a22ced57a46e5652fb35f1d.tar.gz rust-b8eb6ad03273d42d6a22ced57a46e5652fb35f1d.zip | |
std: suggest OnceLock over Once
| -rw-r--r-- | library/std/src/sync/mod.rs | 3 | ||||
| -rw-r--r-- | library/std/src/sync/once.rs | 15 |
2 files changed, 14 insertions, 4 deletions
diff --git a/library/std/src/sync/mod.rs b/library/std/src/sync/mod.rs index 2a13b1a8ae2..9a38c42f43a 100644 --- a/library/std/src/sync/mod.rs +++ b/library/std/src/sync/mod.rs @@ -133,7 +133,8 @@ //! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at //! most one thread at a time is able to access some data. //! -//! - [`Once`]: Used for a thread-safe, one-time global initialization routine +//! - [`Once`]: Used for a thread-safe, one-time global initialization routine. +//! Mostly useful for implementing other types like `OnceLock`. //! //! - [`OnceLock`]: Used for thread-safe, one-time initialization of a //! variable, with potentially different initializers based on the caller. diff --git a/library/std/src/sync/once.rs b/library/std/src/sync/once.rs index 608229fd674..9d969af8c6d 100644 --- a/library/std/src/sync/once.rs +++ b/library/std/src/sync/once.rs @@ -10,9 +10,15 @@ use crate::fmt; use crate::panic::{RefUnwindSafe, UnwindSafe}; use crate::sys::sync as sys; -/// A synchronization primitive which can be used to run a one-time global -/// initialization. Useful for one-time initialization for FFI or related -/// functionality. This type can only be constructed with [`Once::new()`]. +/// A low-level synchronization primitive for one-time global execution. +/// +/// Previously this was the only "execute once" synchronization in `std`. +/// Other libraries implemented novel synchronizing types with `Once`, like +/// [`OnceLock<T>`] or [`LazyLock<T, F>`], before those were added to `std`. +/// `OnceLock<T>` in particular supersedes `Once` in functionality and should +/// be preferred for the common case where the `Once` is associated with data. +/// +/// This type can only be constructed with [`Once::new()`]. /// /// # Examples /// @@ -25,6 +31,9 @@ use crate::sys::sync as sys; /// // run initialization here /// }); /// ``` +/// +/// [`OnceLock<T>`]: crate::sync::OnceLock +/// [`LazyLock<T, F>`]: crate::sync::LazyLock #[stable(feature = "rust1", since = "1.0.0")] pub struct Once { inner: sys::Once, |
