From 21f5f108e00a7fa9c5419b1ae8b92ba91845c35d Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Fri, 21 Jul 2023 18:05:58 +1000 Subject: [PATCH] Make `{Mutex, Notify, OnceCell, RwLock, Semaphore}::const_new` available on rust-version >= 1.63 From rust version 1.63, `Mutex::new` is usable in const context, however `tokio` would still require feature `parking_lot` to be enabled for these functions to be available. This patch makes these function available on rust-version >= 1.63 or if feature `parking_lot` is enabled. Signed-off-by: Jiahao XU --- tokio/src/sync/batch_semaphore.rs | 5 ++++- tokio/src/sync/mutex.rs | 10 ++++++++-- tokio/src/sync/notify.rs | 10 ++++++++-- tokio/src/sync/once_cell.rs | 10 ++++++++-- tokio/src/sync/rwlock.rs | 10 ++++++++-- tokio/src/sync/semaphore.rs | 9 +++++++-- 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index a762f799d56..030095a4ce0 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -181,7 +181,10 @@ impl Semaphore { /// /// If the specified number of permits exceeds the maximum permit amount /// Then the value will get clamped to the maximum number of permits. - #[cfg(all(feature = "parking_lot", not(all(loom, test))))] + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] pub(crate) const fn const_new(mut permits: usize) -> Self { // NOTE: assertions and by extension panics are still being worked on: https://github.com/rust-lang/rust/issues/74925 // currently we just clamp the permit count when it exceeds the max diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 549c77b321e..561850763a3 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -378,8 +378,14 @@ impl Mutex { /// /// static LOCK: Mutex = Mutex::const_new(5); /// ``` - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] - #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] + /// + /// NOTE that this function is available if your rust compiler + /// is newer than 1.63 or if you enabled feature `parking_lot`. + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "parking_lot"))))] pub const fn const_new(t: T) -> Self where T: Sized, diff --git a/tokio/src/sync/notify.rs b/tokio/src/sync/notify.rs index 0f104b71aa2..8cf1659cece 100644 --- a/tokio/src/sync/notify.rs +++ b/tokio/src/sync/notify.rs @@ -443,8 +443,14 @@ impl Notify { /// /// static NOTIFY: Notify = Notify::const_new(); /// ``` - #[cfg(all(feature = "parking_lot", not(all(loom, test))))] - #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] + /// + /// NOTE that this function is available if your rust compiler + /// is newer than 1.63 or if you enabled feature `parking_lot`. + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "parking_lot"))))] pub const fn const_new() -> Notify { Notify { state: AtomicUsize::new(0), diff --git a/tokio/src/sync/once_cell.rs b/tokio/src/sync/once_cell.rs index 90ea5cd6862..d215cb956d8 100644 --- a/tokio/src/sync/once_cell.rs +++ b/tokio/src/sync/once_cell.rs @@ -171,8 +171,14 @@ impl OnceCell { /// assert_eq!(*result, 2); /// } /// ``` - #[cfg(all(feature = "parking_lot", not(all(loom, test))))] - #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] + /// + /// NOTE that this function is available if your rust compiler + /// is newer than 1.63 or if you enabled feature `parking_lot`. + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "parking_lot"))))] pub const fn const_new() -> Self { OnceCell { value_set: AtomicBool::new(false), diff --git a/tokio/src/sync/rwlock.rs b/tokio/src/sync/rwlock.rs index dd4928546fc..6b5878c6a20 100644 --- a/tokio/src/sync/rwlock.rs +++ b/tokio/src/sync/rwlock.rs @@ -334,8 +334,14 @@ impl RwLock { /// /// static LOCK: RwLock = RwLock::const_new(5); /// ``` - #[cfg(all(feature = "parking_lot", not(all(loom, test))))] - #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] + /// + /// NOTE that this function is available if your rust compiler + /// is newer than 1.63 or if you enabled feature `parking_lot`. + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "parking_lot"))))] pub const fn const_new(value: T) -> RwLock where T: Sized, diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index e679d0e6b04..77ca157bd4e 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -173,8 +173,13 @@ impl Semaphore { /// static SEM: Semaphore = Semaphore::const_new(10); /// ``` /// - #[cfg(all(feature = "parking_lot", not(all(loom, test))))] - #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] + /// NOTE that this function is available if your rust compiler + /// is newer than 1.63 or if you enabled feature `parking_lot`. + #[cfg(all( + any(feature = "parking_lot", not(tokio_no_const_mutex_new)), + not(all(loom, test)) + ))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "parking_lot"))))] pub const fn const_new(permits: usize) -> Self { #[cfg(all(tokio_unstable, feature = "tracing"))] return Self {