diff options
| author | bors <bors@rust-lang.org> | 2022-06-19 08:20:36 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-06-19 08:20:36 +0000 |
| commit | 15fc228d0d0a68b5ba565758fab13ed7f863fcea (patch) | |
| tree | e7b2995928c0484ecc6c82a443a3533226aba222 /library/std/src/sync | |
| parent | 5fb8a3926619a18a2e31627e4cdf05f16530b0eb (diff) | |
| parent | edae4958556a6150841b9964b322d1e96e7a4586 (diff) | |
| download | rust-15fc228d0d0a68b5ba565758fab13ed7f863fcea.tar.gz rust-15fc228d0d0a68b5ba565758fab13ed7f863fcea.zip | |
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/sync')
| -rw-r--r-- | library/std/src/sync/condvar.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sync/mutex.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sync/poison.rs | 1 | ||||
| -rw-r--r-- | library/std/src/sync/rwlock.rs | 4 |
4 files changed, 10 insertions, 3 deletions
diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index 7ff2f330f8a..eb1e7135a6e 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -122,8 +122,10 @@ 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] - 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 31342a89054..e0d13cd648c 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -213,7 +213,9 @@ impl<T> Mutex<T> { /// let mutex = Mutex::new(0); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(t: T) -> Mutex<T> { + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] + #[inline] + pub const fn new(t: T) -> Mutex<T> { 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 9c918be3387..741312d5537 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 9517e7e1f03..1192c08cb1a 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -146,7 +146,9 @@ impl<T> RwLock<T> { /// let lock = RwLock::new(5); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(t: T) -> RwLock<T> { + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] + #[inline] + pub const fn new(t: T) -> RwLock<T> { RwLock { inner: sys::MovableRwLock::new(), poison: poison::Flag::new(), |
