From acc3ab4e65cfe4c11156ba7b6be0a33625e7626b Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 6 Jun 2022 13:45:02 +0200 Subject: [PATCH 1/2] Make all {Mutex, Condvar, RwLock}::new #[inline]. --- library/std/src/sync/condvar.rs | 3 ++- library/std/src/sync/mutex.rs | 3 ++- library/std/src/sync/poison.rs | 1 + library/std/src/sync/rwlock.rs | 3 ++- library/std/src/sys/itron/mutex.rs | 1 + library/std/src/sys/solid/rwlock.rs | 1 + library/std/src/sys/unsupported/locks/condvar.rs | 1 + library/std/src/sys/unsupported/locks/mutex.rs | 1 + library/std/src/sys/unsupported/locks/rwlock.rs | 1 + library/std/src/sys/windows/locks/condvar.rs | 1 + library/std/src/sys/windows/locks/mutex.rs | 1 + library/std/src/sys/windows/locks/rwlock.rs | 1 + library/std/src/sys_common/condvar.rs | 3 ++- library/std/src/sys_common/mutex.rs | 2 ++ library/std/src/sys_common/rwlock.rs | 2 ++ 15 files changed, 21 insertions(+), 4 deletions(-) diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index 7ff2f330f8a56..a1d7b6ff06174 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -123,7 +123,8 @@ impl Condvar { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub fn new() -> Condvar { + #[inline] + pub const fn new() -> Condvar { Condvar { inner: sys::Condvar::new() } } diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index 3d8281fe59389..d63f81e68c815 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -214,7 +214,8 @@ impl Mutex { /// let mutex = Mutex::new(0); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(t: T) -> Mutex { + #[inline] + pub const fn new(t: T) -> Mutex { Mutex { inner: sys::MovableMutex::new(), poison: poison::Flag::new(), diff --git a/library/std/src/sync/poison.rs b/library/std/src/sync/poison.rs index ba91fb0499ff0..adef1552016f8 100644 --- a/library/std/src/sync/poison.rs +++ b/library/std/src/sync/poison.rs @@ -19,6 +19,7 @@ pub struct Flag { // all cases. impl Flag { + #[inline] pub const fn new() -> Flag { Flag { failed: AtomicBool::new(false) } } diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index 4f1b4bedaab25..ce62c022ae08b 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -146,7 +146,8 @@ impl RwLock { /// let lock = RwLock::new(5); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(t: T) -> RwLock { + #[inline] + pub const fn new(t: T) -> RwLock { RwLock { inner: sys::MovableRwLock::new(), poison: poison::Flag::new(), diff --git a/library/std/src/sys/itron/mutex.rs b/library/std/src/sys/itron/mutex.rs index 2ba8454ff9245..715e94c3b3d6a 100644 --- a/library/std/src/sys/itron/mutex.rs +++ b/library/std/src/sys/itron/mutex.rs @@ -26,6 +26,7 @@ fn new_mtx() -> Result { } impl Mutex { + #[inline] pub const fn new() -> Mutex { Mutex { mtx: SpinIdOnceCell::new() } } diff --git a/library/std/src/sys/solid/rwlock.rs b/library/std/src/sys/solid/rwlock.rs index 433abc895f5d5..0a770cf03f2f5 100644 --- a/library/std/src/sys/solid/rwlock.rs +++ b/library/std/src/sys/solid/rwlock.rs @@ -23,6 +23,7 @@ fn new_rwl() -> Result { } impl RwLock { + #[inline] pub const fn new() -> RwLock { RwLock { rwl: SpinIdOnceCell::new() } } diff --git a/library/std/src/sys/unsupported/locks/condvar.rs b/library/std/src/sys/unsupported/locks/condvar.rs index f27bf2b26bdaa..e703fd0d26993 100644 --- a/library/std/src/sys/unsupported/locks/condvar.rs +++ b/library/std/src/sys/unsupported/locks/condvar.rs @@ -6,6 +6,7 @@ pub struct Condvar {} pub type MovableCondvar = Condvar; impl Condvar { + #[inline] pub const fn new() -> Condvar { Condvar {} } diff --git a/library/std/src/sys/unsupported/locks/mutex.rs b/library/std/src/sys/unsupported/locks/mutex.rs index 56bad71b189f5..d7cb12e0cf9a4 100644 --- a/library/std/src/sys/unsupported/locks/mutex.rs +++ b/library/std/src/sys/unsupported/locks/mutex.rs @@ -11,6 +11,7 @@ unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} // no threads on this platform impl Mutex { + #[inline] pub const fn new() -> Mutex { Mutex { locked: Cell::new(false) } } diff --git a/library/std/src/sys/unsupported/locks/rwlock.rs b/library/std/src/sys/unsupported/locks/rwlock.rs index bf6e2d3d080b4..aca5fb7152c99 100644 --- a/library/std/src/sys/unsupported/locks/rwlock.rs +++ b/library/std/src/sys/unsupported/locks/rwlock.rs @@ -11,6 +11,7 @@ unsafe impl Send for RwLock {} unsafe impl Sync for RwLock {} // no threads on this platform impl RwLock { + #[inline] pub const fn new() -> RwLock { RwLock { mode: Cell::new(0) } } diff --git a/library/std/src/sys/windows/locks/condvar.rs b/library/std/src/sys/windows/locks/condvar.rs index 59e2c1be0f0f2..be9a2abbe35d9 100644 --- a/library/std/src/sys/windows/locks/condvar.rs +++ b/library/std/src/sys/windows/locks/condvar.rs @@ -14,6 +14,7 @@ unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} impl Condvar { + #[inline] pub const fn new() -> Condvar { Condvar { inner: UnsafeCell::new(c::CONDITION_VARIABLE_INIT) } } diff --git a/library/std/src/sys/windows/locks/mutex.rs b/library/std/src/sys/windows/locks/mutex.rs index 08f55844a0efa..f91e8f9f59a14 100644 --- a/library/std/src/sys/windows/locks/mutex.rs +++ b/library/std/src/sys/windows/locks/mutex.rs @@ -33,6 +33,7 @@ pub unsafe fn raw(m: &Mutex) -> c::PSRWLOCK { } impl Mutex { + #[inline] pub const fn new() -> Mutex { Mutex { srwlock: UnsafeCell::new(c::SRWLOCK_INIT) } } diff --git a/library/std/src/sys/windows/locks/rwlock.rs b/library/std/src/sys/windows/locks/rwlock.rs index a32df85e2f63c..fa5ffe5749f25 100644 --- a/library/std/src/sys/windows/locks/rwlock.rs +++ b/library/std/src/sys/windows/locks/rwlock.rs @@ -11,6 +11,7 @@ unsafe impl Send for RwLock {} unsafe impl Sync for RwLock {} impl RwLock { + #[inline] pub const fn new() -> RwLock { RwLock { inner: UnsafeCell::new(c::SRWLOCK_INIT) } } diff --git a/library/std/src/sys_common/condvar.rs b/library/std/src/sys_common/condvar.rs index 1def0518e0a6f..f3ac1061b8935 100644 --- a/library/std/src/sys_common/condvar.rs +++ b/library/std/src/sys_common/condvar.rs @@ -14,7 +14,8 @@ pub struct Condvar { impl Condvar { /// Creates a new condition variable for use. - pub fn new() -> Self { + #[inline] + pub const fn new() -> Self { Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() } } diff --git a/library/std/src/sys_common/mutex.rs b/library/std/src/sys_common/mutex.rs index 36ea888d8de49..daba9c3f2b69c 100644 --- a/library/std/src/sys_common/mutex.rs +++ b/library/std/src/sys_common/mutex.rs @@ -15,6 +15,7 @@ unsafe impl Sync for StaticMutex {} impl StaticMutex { /// Creates a new mutex for use. + #[inline] pub const fn new() -> Self { Self(imp::Mutex::new()) } @@ -60,6 +61,7 @@ unsafe impl Sync for MovableMutex {} impl MovableMutex { /// Creates a new mutex. + #[inline] pub fn new() -> Self { Self(imp::MovableMutex::new()) } diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs index abc9fd561f1f6..b455074709288 100644 --- a/library/std/src/sys_common/rwlock.rs +++ b/library/std/src/sys_common/rwlock.rs @@ -10,6 +10,7 @@ pub struct StaticRwLock(imp::RwLock); impl StaticRwLock { /// Creates a new rwlock for use. + #[inline] pub const fn new() -> Self { Self(imp::RwLock::new()) } @@ -73,6 +74,7 @@ pub struct MovableRwLock(imp::MovableRwLock); impl MovableRwLock { /// Creates a new reader-writer lock for use. + #[inline] pub fn new() -> Self { Self(imp::MovableRwLock::new()) } From edae4958556a6150841b9964b322d1e96e7a4586 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 6 Jun 2022 13:55:43 +0200 Subject: [PATCH 2/2] Make {Mutex, Condvar, RwLock}::new() const. --- library/std/src/sync/condvar.rs | 1 + library/std/src/sync/mutex.rs | 1 + library/std/src/sync/rwlock.rs | 1 + library/std/src/sys_common/mutex.rs | 2 +- library/std/src/sys_common/rwlock.rs | 2 +- 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index a1d7b6ff06174..eb1e7135a6e41 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -122,6 +122,7 @@ impl Condvar { /// let condvar = Condvar::new(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] #[must_use] #[inline] pub const fn new() -> Condvar { diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index d63f81e68c815..37d4454534d17 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -214,6 +214,7 @@ impl Mutex { /// let mutex = Mutex::new(0); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] #[inline] pub const fn new(t: T) -> Mutex { Mutex { diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index ce62c022ae08b..9fee42e236215 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -146,6 +146,7 @@ impl RwLock { /// let lock = RwLock::new(5); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] #[inline] pub const fn new(t: T) -> RwLock { RwLock { diff --git a/library/std/src/sys_common/mutex.rs b/library/std/src/sys_common/mutex.rs index daba9c3f2b69c..81eefa1133fa5 100644 --- a/library/std/src/sys_common/mutex.rs +++ b/library/std/src/sys_common/mutex.rs @@ -62,7 +62,7 @@ unsafe impl Sync for MovableMutex {} impl MovableMutex { /// Creates a new mutex. #[inline] - pub fn new() -> Self { + pub const fn new() -> Self { Self(imp::MovableMutex::new()) } diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs index b455074709288..265cebfdc3e0a 100644 --- a/library/std/src/sys_common/rwlock.rs +++ b/library/std/src/sys_common/rwlock.rs @@ -75,7 +75,7 @@ pub struct MovableRwLock(imp::MovableRwLock); impl MovableRwLock { /// Creates a new reader-writer lock for use. #[inline] - pub fn new() -> Self { + pub const fn new() -> Self { Self(imp::MovableRwLock::new()) }