Skip to content

Commit

Permalink
Make {Mutex, Notify, OnceCell, RwLock, Semaphore}::const_new availa…
Browse files Browse the repository at this point in the history
…ble 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]>
  • Loading branch information
NobodyXu committed Jul 21, 2023
1 parent 63577cd commit 855718e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
5 changes: 4 additions & 1 deletion tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions tokio/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,18 @@ impl<T: ?Sized> Mutex<T> {
///
/// 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")))]
///
/// NOTE that this function is available if your rust compiler
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
/// 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", not(tokio_no_const_mutex_new))))
)]
pub const fn const_new(t: T) -> Self
where
T: Sized,
Expand Down
14 changes: 12 additions & 2 deletions tokio/src/sync/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,18 @@ 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 and does not specify `--cfg tokio_no_const_mutex_new`
/// 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", not(tokio_no_const_mutex_new))))
)]
pub const fn const_new() -> Notify {
Notify {
state: AtomicUsize::new(0),
Expand Down
14 changes: 12 additions & 2 deletions tokio/src/sync/once_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,18 @@ impl<T> OnceCell<T> {
/// 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 and does not specify `--cfg tokio_no_const_mutex_new`
/// 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", not(tokio_no_const_mutex_new))))
)]
pub const fn const_new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
Expand Down
14 changes: 12 additions & 2 deletions tokio/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,18 @@ impl<T: ?Sized> RwLock<T> {
///
/// 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")))]
///
/// NOTE that this function is available if your rust compiler
/// is newer than 1.63 and does not specify `--cfg tokio_no_const_mutex_new`
/// 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", not(tokio_no_const_mutex_new))))
)]
pub const fn const_new(value: T) -> RwLock<T>
where
T: Sized,
Expand Down
13 changes: 11 additions & 2 deletions tokio/src/sync/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,17 @@ 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 and does not specify `--cfg tokio_no_const_mutex_new`
/// 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", not(tokio_no_const_mutex_new))))
)]
pub const fn const_new(permits: usize) -> Self {
#[cfg(all(tokio_unstable, feature = "tracing"))]
return Self {
Expand Down

0 comments on commit 855718e

Please sign in to comment.