Skip to content

Commit

Permalink
Merge pull request #426 from nspin/pr/from-raw
Browse files Browse the repository at this point in the history
Add `lock_api::{Mutex, ReentrantMutex, RwLock}::from_raw` methods
  • Loading branch information
Amanieu authored Jan 29, 2024
2 parents adbad82 + 8c7aef2 commit 2c1cc2d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
14 changes: 11 additions & 3 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,23 @@ impl<R: RawMutex, T> Mutex<R, T> {

impl<R, T> Mutex<R, T> {
/// Creates a new mutex based on a pre-existing raw mutex.
///
/// This allows creating a mutex in a constant context on stable Rust.
#[inline]
pub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T> {
pub const fn from_raw(raw_mutex: R, val: T) -> Mutex<R, T> {
Mutex {
raw: raw_mutex,
data: UnsafeCell::new(val),
}
}

/// Creates a new mutex based on a pre-existing raw mutex.
///
/// This allows creating a mutex in a constant context on stable Rust.
///
/// This method is a legacy alias for [`from_raw`](Self::from_raw).
#[inline]
pub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T> {
Self::from_raw(raw_mutex, val)
}
}

impl<R: RawMutex, T: ?Sized> Mutex<R, T> {
Expand Down
17 changes: 13 additions & 4 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,8 @@ impl<R: RawMutex, G: GetThreadId, T> ReentrantMutex<R, G, T> {
impl<R, G, T> ReentrantMutex<R, G, T> {
/// Creates a new reentrant mutex based on a pre-existing raw mutex and a
/// helper to get the thread ID.
///
/// This allows creating a reentrant mutex in a constant context on stable
/// Rust.
#[inline]
pub const fn const_new(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex<R, G, T> {
pub const fn from_raw(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex<R, G, T> {
ReentrantMutex {
data: UnsafeCell::new(val),
raw: RawReentrantMutex {
Expand All @@ -283,6 +280,18 @@ impl<R, G, T> ReentrantMutex<R, G, T> {
},
}
}

/// Creates a new reentrant mutex based on a pre-existing raw mutex and a
/// helper to get the thread ID.
///
/// This allows creating a reentrant mutex in a constant context on stable
/// Rust.
///
/// This method is a legacy alias for [`from_raw`](Self::from_raw).
#[inline]
pub const fn const_new(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex<R, G, T> {
Self::from_raw(raw_mutex, get_thread_id, val)
}
}

impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
Expand Down
17 changes: 13 additions & 4 deletions lock_api/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,25 @@ impl<R: RawRwLock, T> RwLock<R, T> {
impl<R, T> RwLock<R, T> {
/// Creates a new new instance of an `RwLock<T>` based on a pre-existing
/// `RawRwLock<T>`.
///
/// This allows creating a `RwLock<T>` in a constant context on stable
/// Rust.
#[inline]
pub const fn const_new(raw_rwlock: R, val: T) -> RwLock<R, T> {
pub const fn from_raw(raw_rwlock: R, val: T) -> RwLock<R, T> {
RwLock {
data: UnsafeCell::new(val),
raw: raw_rwlock,
}
}

/// Creates a new new instance of an `RwLock<T>` based on a pre-existing
/// `RawRwLock<T>`.
///
/// This allows creating a `RwLock<T>` in a constant context on stable
/// Rust.
///
/// This method is a legacy alias for [`from_raw`](Self::from_raw).
#[inline]
pub const fn const_new(raw_rwlock: R, val: T) -> RwLock<R, T> {
Self::from_raw(raw_rwlock, val)
}
}

impl<R: RawRwLock, T: ?Sized> RwLock<R, T> {
Expand Down

0 comments on commit 2c1cc2d

Please sign in to comment.