From 50f7c15e00c5c55604e0cda82cba577588b25360 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Fri, 18 Nov 2022 22:51:18 -0500 Subject: Implemented LockGuard --- src/lock.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/lock.rs') diff --git a/src/lock.rs b/src/lock.rs index 101a061..a8662d0 100644 --- a/src/lock.rs +++ b/src/lock.rs @@ -17,7 +17,7 @@ pub struct Key<'a> { impl<'a> Key<'a> { /// Create a key to a lock. - const fn new(lock: &'a Lock) -> Self { + const unsafe fn new(lock: &'a Lock) -> Self { Self { lock } } } @@ -52,7 +52,8 @@ impl Lock { /// This is not a fair lock. It is not recommended to call this function /// repeatedly in a loop. pub fn try_lock(&self) -> Option { - (!self.is_locked.fetch_or(true, Ordering::Acquire)).then_some(Key::new(self)) + // safety: we just acquired the lock + (!self.is_locked.fetch_or(true, Ordering::Acquire)).then_some(unsafe { Key::new(self) }) } /// Forcibly unlocks the `Lock`. -- cgit v1.2.3