From 8c7aef2044aaee480d1009413c2bb2d405f90160 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Fri, 26 Jan 2024 10:54:00 +0000 Subject: [PATCH] Add lock_api::*::from_raw methods Adds from_raw methods for lock_api::{Mutex, ReentrantMutex, RwLock}. These have the same behavior as the existing legacy const_new methods. --- lock_api/src/mutex.rs | 14 +++++++++++--- lock_api/src/remutex.rs | 17 +++++++++++++---- lock_api/src/rwlock.rs | 17 +++++++++++++---- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lock_api/src/mutex.rs b/lock_api/src/mutex.rs index 8f11e749..ea4b1426 100644 --- a/lock_api/src/mutex.rs +++ b/lock_api/src/mutex.rs @@ -173,15 +173,23 @@ impl Mutex { impl Mutex { /// 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 { + pub const fn from_raw(raw_mutex: R, val: T) -> Mutex { 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 { + Self::from_raw(raw_mutex, val) + } } impl Mutex { diff --git a/lock_api/src/remutex.rs b/lock_api/src/remutex.rs index 80e38bf7..3738c5d7 100644 --- a/lock_api/src/remutex.rs +++ b/lock_api/src/remutex.rs @@ -268,11 +268,8 @@ impl ReentrantMutex { impl ReentrantMutex { /// 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 { + pub const fn from_raw(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex { ReentrantMutex { data: UnsafeCell::new(val), raw: RawReentrantMutex { @@ -283,6 +280,18 @@ impl ReentrantMutex { }, } } + + /// 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 { + Self::from_raw(raw_mutex, get_thread_id, val) + } } impl ReentrantMutex { diff --git a/lock_api/src/rwlock.rs b/lock_api/src/rwlock.rs index e9c6e997..67790b6c 100644 --- a/lock_api/src/rwlock.rs +++ b/lock_api/src/rwlock.rs @@ -396,16 +396,25 @@ impl RwLock { impl RwLock { /// Creates a new new instance of an `RwLock` based on a pre-existing /// `RawRwLock`. - /// - /// This allows creating a `RwLock` in a constant context on stable - /// Rust. #[inline] - pub const fn const_new(raw_rwlock: R, val: T) -> RwLock { + pub const fn from_raw(raw_rwlock: R, val: T) -> RwLock { RwLock { data: UnsafeCell::new(val), raw: raw_rwlock, } } + + /// Creates a new new instance of an `RwLock` based on a pre-existing + /// `RawRwLock`. + /// + /// This allows creating a `RwLock` 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 { + Self::from_raw(raw_rwlock, val) + } } impl RwLock {