diff options
| author | Mica White <botahamec@outlook.com> | 2024-03-08 11:45:15 -0500 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2024-03-08 11:45:15 -0500 |
| commit | 6b4951ade670acbe3cb34b2002fbcd4b4e6a7300 (patch) | |
| tree | c800e9888ba649e2cff27cc1b8ae001c3632572d /src/lib.rs | |
| parent | cff337867884fc5d9eff80c77d41e64ee53c6115 (diff) | |
Replace ownership with mutable access
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 50 |
1 files changed, 1 insertions, 49 deletions
@@ -3,60 +3,12 @@ #![allow(clippy::module_name_repetitions)] #![allow(clippy::declare_interior_mutable_const)] -use std::any::type_name; -use std::fmt::{self, Debug}; -use std::marker::PhantomData; - -use once_cell::sync::Lazy; -use thread_local::ThreadLocal; - mod guard; mod lock; mod lockable; pub mod mutex; -use lock::{Key, Lock}; - pub use guard::LockGuard; +pub use lock::{Key, ThreadKey}; pub use lockable::Lockable; pub use mutex::Mutex; - -static KEY: Lazy<ThreadLocal<Lock>> = Lazy::new(ThreadLocal::new); - -/// The key for the current thread. -/// -/// Only one of these exist per thread. To get the current thread's key, call -/// [`ThreadKey::lock`]. -pub struct ThreadKey { - phantom: PhantomData<*const ()>, // implement !Send and !Sync - _key: Key<'static>, -} - -impl Debug for ThreadKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - type_name::<Self>().fmt(f) - } -} - -impl ThreadKey { - /// Get the current thread's `ThreadKey`, if it's not already taken. - /// - /// The first time this is called, it will successfully return a - /// `ThreadKey`. However, future calls to this function will return - /// [`None`], unless the key is dropped or unlocked first. - #[must_use] - pub fn lock() -> Option<Self> { - KEY.get_or_default().try_lock().map(|key| Self { - phantom: PhantomData, - _key: key, - }) - } - - /// Unlocks the `ThreadKey`. - /// - /// After this method is called, a call to [`ThreadKey::lock`] will return - /// this `ThreadKey`. - pub fn unlock(key: Self) { - drop(key); - } -} |
