diff options
| author | bors <bors@rust-lang.org> | 2024-08-01 00:03:28 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-08-01 00:03:28 +0000 |
| commit | 71b211609605590423a68ba3f503e53404d4fdb4 (patch) | |
| tree | 4a7c2647064201e17116cd74dc56b33d0a0f68bf /library/std/src/sync/once_lock.rs | |
| parent | 28a58f2fa7f0c46b8fab8237c02471a915924fe5 (diff) | |
| parent | cf900ab62d4190be8fbe6448a2d604b951330b2d (diff) | |
| download | rust-71b211609605590423a68ba3f503e53404d4fdb4.tar.gz rust-71b211609605590423a68ba3f503e53404d4fdb4.zip | |
Auto merge of #128469 - matthiaskrgr:rollup-00svite, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #127567 (std: implement the `once_wait` feature) - #128162 (Cleanup sys module to match house style) - #128296 (Update target-spec metadata for loongarch64 targets) - #128443 (Properly mark loop as diverging if it has no breaks) - #128449 (Temporarily switch `ambiguous_negative_literals` lint to allow) - #128452 (derive(SmartPointer): require pointee to be maybe sized) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src/sync/once_lock.rs')
| -rw-r--r-- | library/std/src/sync/once_lock.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs index 60e43a1cde1..efc1f415edf 100644 --- a/library/std/src/sync/once_lock.rs +++ b/library/std/src/sync/once_lock.rs @@ -167,6 +167,34 @@ impl<T> OnceLock<T> { } } + /// Blocks the current thread until the cell is initialized. + /// + /// # Example + /// + /// Waiting for a computation on another thread to finish: + /// ```rust + /// #![feature(once_wait)] + /// + /// use std::thread; + /// use std::sync::OnceLock; + /// + /// let value = OnceLock::new(); + /// + /// thread::scope(|s| { + /// s.spawn(|| value.set(1 + 1)); + /// + /// let result = value.wait(); + /// assert_eq!(result, &2); + /// }) + /// ``` + #[inline] + #[unstable(feature = "once_wait", issue = "127527")] + pub fn wait(&self) -> &T { + self.once.wait_force(); + + unsafe { self.get_unchecked() } + } + /// Sets the contents of this cell to `value`. /// /// May block if another thread is currently attempting to initialize the cell. The cell is |
