about summary refs log tree commit diff
path: root/library/std/src/sys_common
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-06-19 08:20:36 +0000
committerbors <bors@rust-lang.org>2022-06-19 08:20:36 +0000
commit15fc228d0d0a68b5ba565758fab13ed7f863fcea (patch)
treee7b2995928c0484ecc6c82a443a3533226aba222 /library/std/src/sys_common
parent5fb8a3926619a18a2e31627e4cdf05f16530b0eb (diff)
parentedae4958556a6150841b9964b322d1e96e7a4586 (diff)
Auto merge of #97791 - m-ou-se:const-locks, r=m-ou-se
Make {Mutex, Condvar, RwLock}::new() const.

This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉

Our implementations [on Linux](https://github.com/rust-lang/rust/pull/95035), [on Windows](https://github.com/rust-lang/rust/pull/77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of https://github.com/rust-lang/rust/pull/97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const.

Tracking issue: https://github.com/rust-lang/rust/issues/93740
Diffstat (limited to 'library/std/src/sys_common')
-rw-r--r--library/std/src/sys_common/condvar.rs3
-rw-r--r--library/std/src/sys_common/mutex.rs4
-rw-r--r--library/std/src/sys_common/rwlock.rs4
3 files changed, 8 insertions, 3 deletions
diff --git a/library/std/src/sys_common/condvar.rs b/library/std/src/sys_common/condvar.rs
index 1def0518e0a..f3ac1061b89 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 36ea888d8de..81eefa1133f 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,7 +61,8 @@ unsafe impl Sync for MovableMutex {}
 
 impl MovableMutex {
     /// Creates a new mutex.
-    pub fn new() -> Self {
+    #[inline]
+    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 abc9fd561f1..265cebfdc3e 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,7 +74,8 @@ pub struct MovableRwLock(imp::MovableRwLock);
 
 impl MovableRwLock {
     /// Creates a new reader-writer lock for use.
-    pub fn new() -> Self {
+    #[inline]
+    pub const fn new() -> Self {
         Self(imp::MovableRwLock::new())
     }