summaryrefslogtreecommitdiffstats
path: root/library/std/src/sync
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /library/std/src/sync
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/sync')
-rw-r--r--library/std/src/sync/mpsc/sync_tests.rs1
-rw-r--r--library/std/src/sync/mpsc/tests.rs1
-rw-r--r--library/std/src/sync/mutex.rs2
-rw-r--r--library/std/src/sync/once_lock.rs61
-rw-r--r--library/std/src/sync/rwlock.rs4
5 files changed, 55 insertions, 14 deletions
diff --git a/library/std/src/sync/mpsc/sync_tests.rs b/library/std/src/sync/mpsc/sync_tests.rs
index 632709fd9..945de280f 100644
--- a/library/std/src/sync/mpsc/sync_tests.rs
+++ b/library/std/src/sync/mpsc/sync_tests.rs
@@ -3,7 +3,6 @@ use crate::env;
use crate::rc::Rc;
use crate::sync::mpmc::SendTimeoutError;
use crate::thread;
-use crate::time::Duration;
pub fn stress_factor() -> usize {
match env::var("RUST_TEST_STRESS") {
diff --git a/library/std/src/sync/mpsc/tests.rs b/library/std/src/sync/mpsc/tests.rs
index 1e52a4a70..ac1a804cf 100644
--- a/library/std/src/sync/mpsc/tests.rs
+++ b/library/std/src/sync/mpsc/tests.rs
@@ -1,7 +1,6 @@
use super::*;
use crate::env;
use crate::thread;
-use crate::time::{Duration, Instant};
pub fn stress_factor() -> usize {
match env::var("RUST_TEST_STRESS") {
diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs
index b4ae6b7e0..0c001d7c2 100644
--- a/library/std/src/sync/mutex.rs
+++ b/library/std/src/sync/mutex.rs
@@ -146,7 +146,7 @@ use crate::sys::locks as sys;
/// let result = data.iter().fold(0, |acc, x| acc + x * 2);
/// data.push(result);
/// // We drop the `data` explicitly because it's not necessary anymore and the
-/// // thread still has work to do. This allow other threads to start working on
+/// // thread still has work to do. This allows other threads to start working on
/// // the data immediately, without waiting for the rest of the unrelated work
/// // to be done here.
/// //
diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs
index f49630907..b8873a3b5 100644
--- a/library/std/src/sync/once_lock.rs
+++ b/library/std/src/sync/once_lock.rs
@@ -13,22 +13,65 @@ use crate::sync::Once;
///
/// # Examples
///
+/// Using `OnceCell` to store a function’s previously computed value (a.k.a.
+/// ‘lazy static’ or ‘memoizing’):
+///
/// ```
/// use std::sync::OnceLock;
///
-/// static CELL: OnceLock<String> = OnceLock::new();
+/// struct DeepThought {
+/// answer: String,
+/// }
+///
+/// impl DeepThought {
+/// # fn great_question() -> String {
+/// # "42".to_string()
+/// # }
+/// #
+/// fn new() -> Self {
+/// Self {
+/// // M3 Ultra takes about 16 million years in --release config
+/// answer: Self::great_question(),
+/// }
+/// }
+/// }
+///
+/// fn computation() -> &'static DeepThought {
+/// // n.b. static items do not call [`Drop`] on program termination, so if
+/// // [`DeepThought`] impls Drop, that will not be used for this instance.
+/// static COMPUTATION: OnceLock<DeepThought> = OnceLock::new();
+/// COMPUTATION.get_or_init(|| DeepThought::new())
+/// }
+///
+/// // The `DeepThought` is built, stored in the `OnceLock`, and returned.
+/// let _ = computation().answer;
+/// // The `DeepThought` is retrieved from the `OnceLock` and returned.
+/// let _ = computation().answer;
+/// ```
+///
+/// Writing to a `OnceLock` from a separate thread:
+///
+/// ```
+/// use std::sync::OnceLock;
+///
+/// static CELL: OnceLock<usize> = OnceLock::new();
+///
+/// // `OnceLock` has not been written to yet.
/// assert!(CELL.get().is_none());
///
+/// // Spawn a thread and write to `OnceLock`.
/// std::thread::spawn(|| {
-/// let value: &String = CELL.get_or_init(|| {
-/// "Hello, World!".to_string()
-/// });
-/// assert_eq!(value, "Hello, World!");
-/// }).join().unwrap();
+/// let value = CELL.get_or_init(|| 12345);
+/// assert_eq!(value, &12345);
+/// })
+/// .join()
+/// .unwrap();
///
-/// let value: Option<&String> = CELL.get();
-/// assert!(value.is_some());
-/// assert_eq!(value.unwrap().as_str(), "Hello, World!");
+/// // `OnceLock` now contains the value.
+/// assert_eq!(
+/// CELL.get(),
+/// Some(&12345),
+/// );
/// ```
#[stable(feature = "once_cell", since = "1.70.0")]
pub struct OnceLock<T> {
diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs
index ac7c800ff..5d8967bfb 100644
--- a/library/std/src/sync/rwlock.rs
+++ b/library/std/src/sync/rwlock.rs
@@ -532,7 +532,7 @@ impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
}
#[stable(feature = "std_debug", since = "1.16.0")]
-impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
+impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
@@ -546,7 +546,7 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
}
#[stable(feature = "std_debug", since = "1.16.0")]
-impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
+impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}