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 +++++++++++------- src/mutex/mutex.rs | 9 ++++++--- src/rwlock.rs | 6 +++--- src/rwlock/read_lock.rs | 2 +- src/rwlock/rwlock.rs | 12 ++++++------ src/rwlock/write_lock.rs | 2 +- 9 files changed, 37 insertions(+), 30 deletions(-) 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) + } } } diff --git a/src/mutex/mutex.rs b/src/mutex/mutex.rs index 080e043..d744891 100644 --- a/src/mutex/mutex.rs +++ b/src/mutex/mutex.rs @@ -285,12 +285,15 @@ impl Mutex { pub fn try_lock<'s, 'a: 's, 'k: 'a, Key: Keyable>( &'s self, key: Key, - ) -> Option> { + ) -> Result, Key> { unsafe { // safety: we have the key to the mutex - self.raw_try_lock().then(|| + if self.raw_try_lock() { // safety: we just locked the mutex - MutexGuard::new(self, key)) + Ok(MutexGuard::new(self, key)) + } else { + Err(key) + } } } diff --git a/src/rwlock.rs b/src/rwlock.rs index 9b65a0b..64dc82b 100644 --- a/src/rwlock.rs +++ b/src/rwlock.rs @@ -126,7 +126,7 @@ mod tests { let lock: crate::RwLock<_> = RwLock::new("Hello, world!"); assert!(!lock.is_locked()); - assert!(lock.try_write(key).is_some()); + assert!(lock.try_write(key).is_ok()); } #[test] @@ -135,7 +135,7 @@ mod tests { let lock: crate::RwLock<_> = RwLock::new("Hello, world!"); let reader = ReadLock::new(&lock); - assert!(reader.try_lock(key).is_some()); + assert!(reader.try_lock(key).is_ok()); } #[test] @@ -144,7 +144,7 @@ mod tests { let lock: crate::RwLock<_> = RwLock::new("Hello, world!"); let writer = WriteLock::new(&lock); - assert!(writer.try_lock(key).is_some()); + assert!(writer.try_lock(key).is_ok()); } #[test] diff --git a/src/rwlock/read_lock.rs b/src/rwlock/read_lock.rs index c5c4c8c..e518cf3 100644 --- a/src/rwlock/read_lock.rs +++ b/src/rwlock/read_lock.rs @@ -72,7 +72,7 @@ impl ReadLock<'_, T, R> { pub fn try_lock<'s, 'key: 's, Key: Keyable + 'key>( &'s self, key: Key, - ) -> Option> { + ) -> Result, Key> { self.0.try_read(key) } diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index 03b2cfd..b5dea75 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -286,13 +286,13 @@ impl RwLock { pub fn try_read<'s, 'key: 's, Key: Keyable>( &'s self, key: Key, - ) -> Option> { + ) -> Result, Key> { unsafe { if self.raw_try_read() { // safety: the lock is locked first - Some(RwLockReadGuard::new(self, key)) + Ok(RwLockReadGuard::new(self, key)) } else { - None + Err(key) } } } @@ -381,13 +381,13 @@ impl RwLock { pub fn try_write<'s, 'key: 's, Key: Keyable>( &'s self, key: Key, - ) -> Option> { + ) -> Result, Key> { unsafe { if self.raw_try_lock() { // safety: the lock is locked first - Some(RwLockWriteGuard::new(self, key)) + Ok(RwLockWriteGuard::new(self, key)) } else { - None + Err(key) } } } diff --git a/src/rwlock/write_lock.rs b/src/rwlock/write_lock.rs index 77b68c8..ba05e87 100644 --- a/src/rwlock/write_lock.rs +++ b/src/rwlock/write_lock.rs @@ -73,7 +73,7 @@ impl WriteLock<'_, T, R> { pub fn try_lock<'s, 'key: 's, Key: Keyable + 'key>( &'s self, key: Key, - ) -> Option> { + ) -> Result, Key> { self.0.try_write(key) } -- cgit v1.2.3