Skip to content

Commit c20495b

Browse files
committed
Add convenience const constructors to parking_lot
This adds various helper functions to `parking_lot` that allow creating parking_lot's mutexes, reentrant mutexes, fair mutexes and RwLocks in constant contexts on stable Rust.
1 parent a9961d4 commit c20495b

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/fair_mutex.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ use lock_api;
8080
/// ```
8181
pub type FairMutex<T> = lock_api::Mutex<RawFairMutex, T>;
8282

83+
/// Creates a new fair mutex in an unlocked state ready for use. This allows
84+
/// creating a fair mutex in a constant context on stable Rust.
85+
pub const fn new_fair_mutex<T>(val: T) -> FairMutex<T> {
86+
FairMutex::const_new(<RawFairMutex as lock_api::RawMutex>::INIT, val)
87+
}
88+
8389
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
8490
/// dropped (falls out of scope), the lock will be unlocked.
8591
///

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ mod elision;
1818
mod fair_mutex;
1919
mod mutex;
2020
mod once;
21-
mod raw_mutex;
2221
mod raw_fair_mutex;
22+
mod raw_mutex;
2323
mod raw_rwlock;
2424
mod remutex;
2525
mod rwlock;
@@ -31,17 +31,18 @@ pub mod deadlock;
3131
mod deadlock;
3232

3333
pub use self::condvar::{Condvar, WaitTimeoutResult};
34-
pub use self::mutex::{MappedMutexGuard, Mutex, MutexGuard};
35-
pub use self::fair_mutex::{MappedFairMutexGuard, FairMutex, FairMutexGuard};
34+
pub use self::fair_mutex::{new_fair_mutex, FairMutex, FairMutexGuard, MappedFairMutexGuard};
35+
pub use self::mutex::{new_mutex, MappedMutexGuard, Mutex, MutexGuard};
3636
pub use self::once::{Once, OnceState};
37-
pub use self::raw_mutex::RawMutex;
3837
pub use self::raw_fair_mutex::RawFairMutex;
38+
pub use self::raw_mutex::RawMutex;
3939
pub use self::raw_rwlock::RawRwLock;
4040
pub use self::remutex::{
41-
MappedReentrantMutexGuard, RawThreadId, ReentrantMutex, ReentrantMutexGuard,
41+
new_reentrant_mutex, MappedReentrantMutexGuard, RawThreadId, ReentrantMutex,
42+
ReentrantMutexGuard,
4243
};
4344
pub use self::rwlock::{
44-
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard,
45+
new_rwlock, MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard,
4546
RwLockUpgradableReadGuard, RwLockWriteGuard,
4647
};
4748
pub use ::lock_api;

src/mutex.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ use lock_api;
8686
/// ```
8787
pub type Mutex<T> = lock_api::Mutex<RawMutex, T>;
8888

89+
/// Creates a new mutex in an unlocked state ready for use. This allows creating
90+
/// a mutex in a constant context on stable Rust.
91+
pub const fn new_mutex<T>(val: T) -> Mutex<T> {
92+
Mutex::const_new(<RawMutex as lock_api::RawMutex>::INIT, val)
93+
}
94+
8995
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
9096
/// dropped (falls out of scope), the lock will be unlocked.
9197
///

src/remutex.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ unsafe impl GetThreadId for RawThreadId {
4040
/// primitive.
4141
pub type ReentrantMutex<T> = lock_api::ReentrantMutex<RawMutex, RawThreadId, T>;
4242

43+
/// Creates a new reentrant mutex in an unlocked state ready for use. This
44+
/// allows creating a reentrant mutex in a constant context on stable Rust.
45+
pub const fn new_reentrant_mutex<T>(val: T) -> ReentrantMutex<T> {
46+
ReentrantMutex::const_new(
47+
<RawMutex as lock_api::RawMutex>::INIT,
48+
<RawThreadId as lock_api::GetThreadId>::INIT,
49+
val,
50+
)
51+
}
52+
4353
/// An RAII implementation of a "scoped lock" of a reentrant mutex. When this structure
4454
/// is dropped (falls out of scope), the lock will be unlocked.
4555
///

src/rwlock.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ use lock_api;
8888
/// ```
8989
pub type RwLock<T> = lock_api::RwLock<RawRwLock, T>;
9090

91+
/// Creates a new instance of an `RwLock<T>` which is unlocked. This allows
92+
/// creating a `RwLock<T>` in a constant context on stable Rust.
93+
pub const fn new_rwlock<T>(val: T) -> RwLock<T> {
94+
RwLock::const_new(<RawRwLock as lock_api::RawRwLock>::INIT, val)
95+
}
96+
9197
/// RAII structure used to release the shared read access of a lock when
9298
/// dropped.
9399
pub type RwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawRwLock, T>;

0 commit comments

Comments
 (0)