diff options
author | mental <m3nta1@yahoo.com> | 2020-09-12 10:55:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-12 11:55:03 +0200 |
commit | 20ef28655354ae729a4af2098426a413e3f4d769 (patch) | |
tree | 1122ae239c47b3553f4b50741a756e36de34a707 /tokio/src/sync/mutex.rs | |
parent | 2bc9a4815259c8ff4daa5e24f128ec826970d17f (diff) |
sync: add const-constructors for some sync primitives (#2790)
Co-authored-by: Mikail Bagishov <bagishov.mikail@yandex.ru>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Diffstat (limited to 'tokio/src/sync/mutex.rs')
-rw-r--r-- | tokio/src/sync/mutex.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index df348457..71f0136f 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -219,6 +219,27 @@ impl<T: ?Sized> Mutex<T> { } } + /// Creates a new lock in an unlocked state ready for use. + /// + /// # Examples + /// + /// ``` + /// use tokio::sync::Mutex; + /// + /// static LOCK: Mutex<i32> = Mutex::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(t: T) -> Self + where + T: Sized, + { + Self { + c: UnsafeCell::new(t), + s: semaphore::Semaphore::const_new(1), + } + } + /// Locks this mutex, causing the current task /// to yield until the lock has been acquired. /// When the lock has been acquired, function returns a [`MutexGuard`]. |