Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock_api::{Mutex, ReentrantMutex, RwLock}::from_raw methods #426

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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