From 03d0cdb7145042ce51c9eb98bfd081a4f10fe8e1 Mon Sep 17 00:00:00 2001 From: Mica White Date: Fri, 8 Mar 2024 12:18:41 -0500 Subject: Keyable --- src/guard.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/guard.rs') diff --git a/src/guard.rs b/src/guard.rs index 3c1b636..30e7d4a 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -1,17 +1,17 @@ use std::ops::{Deref, DerefMut}; -use crate::{lockable::Lockable, ThreadKey}; +use crate::{key::Keyable, lockable::Lockable}; /// A guard for a generic [`Lockable`] type. -pub struct LockGuard<'a, L: Lockable<'a>> { +pub struct LockGuard<'a, L: Lockable<'a>, Key: Keyable> { guard: L::Output, - _key: &'a mut ThreadKey, + _key: Key, } -impl<'a, L: Lockable<'a>> LockGuard<'a, L> { +impl<'a, L: Lockable<'a>, Key: Keyable> LockGuard<'a, L, Key> { /// Locks the lockable type and returns a guard that can be used to access /// the underlying data. - pub fn lock(lock: &'a L, key: &'a mut ThreadKey) -> Self { + pub fn lock(lock: &'a L, key: Key) -> Self { Self { // safety: we have the thread's key guard: unsafe { lock.lock() }, @@ -22,7 +22,7 @@ impl<'a, L: Lockable<'a>> LockGuard<'a, L> { /// Attempts to lock the guard without blocking. If successful, this method /// returns a guard that can be used to access the data. Otherwise, the key /// is given back as an error. - pub fn try_lock(lock: &'a L, key: &'a mut ThreadKey) -> Option { + pub fn try_lock(lock: &'a L, key: Key) -> Option { // safety: we have the thread's key unsafe { lock.try_lock() }.map(|guard| Self { guard, _key: key }) } @@ -35,7 +35,7 @@ impl<'a, L: Lockable<'a>> LockGuard<'a, L> { } } -impl<'a, L: Lockable<'a>> Deref for LockGuard<'a, L> { +impl<'a, L: Lockable<'a>, Key: Keyable> Deref for LockGuard<'a, L, Key> { type Target = L::Output; fn deref(&self) -> &Self::Target { @@ -43,7 +43,7 @@ impl<'a, L: Lockable<'a>> Deref for LockGuard<'a, L> { } } -impl<'a, L: Lockable<'a>> DerefMut for LockGuard<'a, L> { +impl<'a, L: Lockable<'a>, Key: Keyable> DerefMut for LockGuard<'a, L, Key> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.guard } -- cgit v1.2.3