Skip to content

Commit 855718e

Browse files
committed
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 given that `--cfg tokio_no_const_mutex_new` is not specified or if feature `parking_lot` is enabled. Signed-off-by: Jiahao XU <[email protected]>
1 parent 63577cd commit 855718e

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

tokio/src/sync/batch_semaphore.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ impl Semaphore {
181181
///
182182
/// If the specified number of permits exceeds the maximum permit amount
183183
/// Then the value will get clamped to the maximum number of permits.
184-
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
184+
#[cfg(all(
185+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
186+
not(all(loom, test))
187+
))]
185188
pub(crate) const fn const_new(mut permits: usize) -> Self {
186189
// NOTE: assertions and by extension panics are still being worked on: https://github.com/rust-lang/rust/issues/74925
187190
// currently we just clamp the permit count when it exceeds the max

tokio/src/sync/mutex.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,18 @@ impl<T: ?Sized> Mutex<T> {
378378
///
379379
/// static LOCK: Mutex<i32> = Mutex::const_new(5);
380380
/// ```
381-
#[cfg(all(feature = "parking_lot", not(all(loom, test)),))]
382-
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
381+
///
382+
/// NOTE that this function is available if your rust compiler
383+
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
384+
/// or if you enabled feature `parking_lot`.
385+
#[cfg(all(
386+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
387+
not(all(loom, test))
388+
))]
389+
#[cfg_attr(
390+
docsrs,
391+
doc(cfg(any(feature = "parking_lot", not(tokio_no_const_mutex_new))))
392+
)]
383393
pub const fn const_new(t: T) -> Self
384394
where
385395
T: Sized,

tokio/src/sync/notify.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,18 @@ impl Notify {
443443
///
444444
/// static NOTIFY: Notify = Notify::const_new();
445445
/// ```
446-
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
447-
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
446+
///
447+
/// NOTE that this function is available if your rust compiler
448+
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
449+
/// or if you enabled feature `parking_lot`.
450+
#[cfg(all(
451+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
452+
not(all(loom, test))
453+
))]
454+
#[cfg_attr(
455+
docsrs,
456+
doc(cfg(any(feature = "parking_lot", not(tokio_no_const_mutex_new))))
457+
)]
448458
pub const fn const_new() -> Notify {
449459
Notify {
450460
state: AtomicUsize::new(0),

tokio/src/sync/once_cell.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,18 @@ impl<T> OnceCell<T> {
171171
/// assert_eq!(*result, 2);
172172
/// }
173173
/// ```
174-
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
175-
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
174+
///
175+
/// NOTE that this function is available if your rust compiler
176+
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
177+
/// or if you enabled feature `parking_lot`.
178+
#[cfg(all(
179+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
180+
not(all(loom, test))
181+
))]
182+
#[cfg_attr(
183+
docsrs,
184+
doc(cfg(any(feature = "parking_lot", not(tokio_no_const_mutex_new))))
185+
)]
176186
pub const fn const_new() -> Self {
177187
OnceCell {
178188
value_set: AtomicBool::new(false),

tokio/src/sync/rwlock.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,18 @@ impl<T: ?Sized> RwLock<T> {
334334
///
335335
/// static LOCK: RwLock<i32> = RwLock::const_new(5);
336336
/// ```
337-
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
338-
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
337+
///
338+
/// NOTE that this function is available if your rust compiler
339+
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
340+
/// or if you enabled feature `parking_lot`.
341+
#[cfg(all(
342+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
343+
not(all(loom, test))
344+
))]
345+
#[cfg_attr(
346+
docsrs,
347+
doc(cfg(any(feature = "parking_lot", not(tokio_no_const_mutex_new))))
348+
)]
339349
pub const fn const_new(value: T) -> RwLock<T>
340350
where
341351
T: Sized,

tokio/src/sync/semaphore.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,17 @@ impl Semaphore {
173173
/// static SEM: Semaphore = Semaphore::const_new(10);
174174
/// ```
175175
///
176-
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
177-
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
176+
/// NOTE that this function is available if your rust compiler
177+
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
178+
/// or if you enabled feature `parking_lot`.
179+
#[cfg(all(
180+
any(feature = "parking_lot", not(tokio_no_const_mutex_new)),
181+
not(all(loom, test))
182+
))]
183+
#[cfg_attr(
184+
docsrs,
185+
doc(cfg(any(feature = "parking_lot", not(tokio_no_const_mutex_new))))
186+
)]
178187
pub const fn const_new(permits: usize) -> Self {
179188
#[cfg(all(tokio_unstable, feature = "tracing"))]
180189
return Self {

0 commit comments

Comments
 (0)