diff options
| -rw-r--r-- | src/collection/boxed.rs | 6 | ||||
| -rw-r--r-- | src/collection/owned.rs | 6 | ||||
| -rw-r--r-- | src/collection/ref.rs | 6 | ||||
| -rw-r--r-- | src/collection/retry.rs | 18 | ||||
| -rw-r--r-- | src/mutex/mutex.rs | 9 | ||||
| -rw-r--r-- | src/rwlock.rs | 6 | ||||
| -rw-r--r-- | src/rwlock/read_lock.rs | 2 | ||||
| -rw-r--r-- | src/rwlock/rwlock.rs | 12 | ||||
| -rw-r--r-- | 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<L: Lockable> BoxedLockCollection<L> { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option<LockGuard<'key, L::Guard<'g>, Key>> { + ) -> Result<LockGuard<'key, L::Guard<'g>, 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<L: OwnedLockable> OwnedLockCollection<L> { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option<LockGuard<'key, L::Guard<'g>, Key>> { + ) -> Result<LockGuard<'key, L::Guard<'g>, 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<LockGuard<'key, L::Guard<'a>, Key>> { + ) -> Result<LockGuard<'key, L::Guard<'a>, 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<L: Lockable> RetryingLockCollection<L> { pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, - ) -> Option<LockGuard<'key, L::Guard<'g>, Key>> { + ) -> Result<LockGuard<'key, L::Guard<'g>, 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<T: ?Sized, R: RawMutex> Mutex<T, R> { pub fn try_lock<'s, 'a: 's, 'k: 'a, Key: Keyable>( &'s self, key: Key, - ) -> Option<MutexGuard<'s, 'k, T, Key, R>> { + ) -> Result<MutexGuard<'s, 'k, T, Key, R>, 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<T: ?Sized, R: RawRwLock> ReadLock<'_, T, R> { pub fn try_lock<'s, 'key: 's, Key: Keyable + 'key>( &'s self, key: Key, - ) -> Option<RwLockReadGuard<'s, 'key, T, Key, R>> { + ) -> Result<RwLockReadGuard<'s, 'key, T, Key, R>, 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<T: ?Sized, R: RawRwLock> RwLock<T, R> { pub fn try_read<'s, 'key: 's, Key: Keyable>( &'s self, key: Key, - ) -> Option<RwLockReadGuard<'s, 'key, T, Key, R>> { + ) -> Result<RwLockReadGuard<'s, 'key, T, Key, R>, 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<T: ?Sized, R: RawRwLock> RwLock<T, R> { pub fn try_write<'s, 'key: 's, Key: Keyable>( &'s self, key: Key, - ) -> Option<RwLockWriteGuard<'s, 'key, T, Key, R>> { + ) -> Result<RwLockWriteGuard<'s, 'key, T, Key, R>, 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<T: ?Sized, R: RawRwLock> WriteLock<'_, T, R> { pub fn try_lock<'s, 'key: 's, Key: Keyable + 'key>( &'s self, key: Key, - ) -> Option<RwLockWriteGuard<'s, 'key, T, Key, R>> { + ) -> Result<RwLockWriteGuard<'s, 'key, T, Key, R>, Key> { self.0.try_write(key) } |
