diff options
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/lib.rs | 10 | ||||
| -rw-r--r-- | src/lock.rs | 7 |
3 files changed, 13 insertions, 6 deletions
@@ -2,6 +2,8 @@ name = "happylock" version = "0.1.0" edition = "2021" +description = "Locks without Deadlocks" +license = "Unlicense" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -1,3 +1,6 @@ +#![warn(clippy::pedantic)] +#![warn(clippy::nursery)] + use std::any::type_name; use std::fmt::{self, Debug}; use std::marker::PhantomData; @@ -26,11 +29,12 @@ impl Debug for ThreadKey { } impl ThreadKey { - /// Get the current thread's `ThreadKey, if it's not already taken. + /// 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, @@ -42,7 +46,7 @@ impl ThreadKey { /// /// After this method is called, a call to [`ThreadKey::lock`] will return /// this `ThreadKey`. - pub fn unlock(key: ThreadKey) { - drop(key) + pub fn unlock(key: Self) { + drop(key); } } diff --git a/src/lock.rs b/src/lock.rs index fb74f25..5a6315d 100644 --- a/src/lock.rs +++ b/src/lock.rs @@ -12,7 +12,7 @@ pub struct Key<'a> { } impl<'a> Key<'a> { - fn new(lock: &'a Lock) -> Self { + const fn new(lock: &'a Lock) -> Self { Self { lock } } } @@ -26,6 +26,7 @@ impl<'a> Drop for Key<'a> { impl Lock { /// Create a new unlocked `Lock`. + #[must_use] pub const fn new() -> Self { Self { is_locked: AtomicBool::new(false), @@ -52,10 +53,10 @@ impl Lock { /// means the program no longer has a reference to the key, but it has not /// been dropped. unsafe fn force_unlock(&self) { - self.is_locked.store(false, Ordering::Release) + self.is_locked.store(false, Ordering::Release); } pub fn unlock(key: Key) { - drop(key) + drop(key); } } |
