From 657377311d3b041ac7b942e61ddbbe2861c494ec Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 25 Dec 2024 11:17:35 -0500 Subject: try_lock returns a Result --- src/collection/boxed.rs | 6 +++--- src/collection/owned.rs | 6 +++--- src/collection/ref.rs | 6 +++--- src/collection/retry.rs | 18 +++++++++++------- 4 files changed, 20 insertions(+), 16 deletions(-) (limited to 'src/collection') diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs index 0014cc3..6db0683 100644 --- a/src/collection/boxed.rs +++ b/src/collection/boxed.rs @@ -372,17 +372,17 @@ impl BoxedLockCollection { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option, Key>> { + ) -> Result, Key>, Key> { let guard = unsafe { if !self.raw_try_lock() { - return None; + return Err(key); } // safety: we've acquired the locks self.data().guard() }; - Some(LockGuard { + Ok(LockGuard { guard, key, _phantom: PhantomData, diff --git a/src/collection/owned.rs b/src/collection/owned.rs index 69680f4..3ea08b5 100644 --- a/src/collection/owned.rs +++ b/src/collection/owned.rs @@ -243,17 +243,17 @@ impl OwnedLockCollection { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option, Key>> { + ) -> Result, Key>, Key> { let guard = unsafe { if !self.raw_try_lock() { - return None; + return Err(key); } // safety: we've acquired the locks self.data.guard() }; - Some(LockGuard { + Ok(LockGuard { guard, key, _phantom: PhantomData, diff --git a/src/collection/ref.rs b/src/collection/ref.rs index b0b142e..1f19e4d 100644 --- a/src/collection/ref.rs +++ b/src/collection/ref.rs @@ -268,17 +268,17 @@ impl<'a, L: Lockable> RefLockCollection<'a, L> { pub fn try_lock<'key: 'a, Key: Keyable + 'key>( &'a self, key: Key, - ) -> Option, Key>> { + ) -> Result, Key>, Key> { let guard = unsafe { if !self.raw_try_lock() { - return None; + return Err(key); } // safety: we've acquired the locks self.data.guard() }; - Some(LockGuard { + Ok(LockGuard { guard, key, _phantom: PhantomData, diff --git a/src/collection/retry.rs b/src/collection/retry.rs index 28602f2..42d86e5 100644 --- a/src/collection/retry.rs +++ b/src/collection/retry.rs @@ -513,15 +513,19 @@ impl RetryingLockCollection { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option, Key>> { + ) -> Result, Key>, Key> { unsafe { // safety: we're taking the thread key - self.raw_try_lock().then(|| LockGuard { - // safety: we just succeeded in locking everything - guard: self.guard(), - key, - _phantom: PhantomData, - }) + if self.raw_try_lock() { + Ok(LockGuard { + // safety: we just succeeded in locking everything + guard: self.guard(), + key, + _phantom: PhantomData, + }) + } else { + Err(key) + } } } -- cgit v1.2.3