summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync/rwlock.rs
diff options
context:
space:
mode:
authorFrank Steffahn <fdsteffahn@gmail.com>2020-09-12 22:58:58 +0200
committerGitHub <noreply@github.com>2020-09-12 22:58:58 +0200
commit8d2e3bc575f51815ae7319f1e43fe6c7d664e6e4 (patch)
tree61fa8e1da832757b996abe7b03be85f156c66d6b /tokio/src/sync/rwlock.rs
parent20ef28655354ae729a4af2098426a413e3f4d769 (diff)
sync: add const constructors to RwLock, Notify, and Semaphore (#2833)
* Add const constructors to `RwLock`, `Notify`, and `Semaphore`. Referring to the types in `tokio::sync`. Also add `const` to `new` for the remaining atomic integers in `src/loom` and `UnsafeCell`. Builds upon previous work in #2790 Closes #2756
Diffstat (limited to 'tokio/src/sync/rwlock.rs')
-rw-r--r--tokio/src/sync/rwlock.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/tokio/src/sync/rwlock.rs b/tokio/src/sync/rwlock.rs
index 650a7cf6..840889ba 100644
--- a/tokio/src/sync/rwlock.rs
+++ b/tokio/src/sync/rwlock.rs
@@ -485,6 +485,27 @@ impl<T: ?Sized> RwLock<T> {
}
}
+ /// Creates a new instance of an `RwLock<T>` which is unlocked.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::RwLock;
+ ///
+ /// static LOCK: RwLock<i32> = RwLock::const_new(5);
+ /// ```
+ #[cfg(all(feature = "parking_lot", not(all(loom, test))))]
+ #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
+ pub const fn const_new(value: T) -> RwLock<T>
+ where
+ T: Sized,
+ {
+ RwLock {
+ c: UnsafeCell::new(value),
+ s: Semaphore::const_new(MAX_READS),
+ }
+ }
+
/// Locks this rwlock with shared read access, causing the current task
/// to yield until the lock has been acquired.
///