summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync/mutex.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-12-18 11:50:56 -0800
committerGitHub <noreply@github.com>2019-12-18 11:50:56 -0800
commitb0836ece7aa5219e9e40355d0eb784baffc7b6c6 (patch)
tree2526ff6dff923262172b2930cc5425311e126f04 /tokio/src/sync/mutex.rs
parent0c0f68201037af3fbaac6ca29a9806e392593d89 (diff)
sync: encapsulate TryLockError variants (#1980)
As there is currently only one variant, make the error type an opaque struct.
Diffstat (limited to 'tokio/src/sync/mutex.rs')
-rw-r--r--tokio/src/sync/mutex.rs24
1 files changed, 8 insertions, 16 deletions
diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs
index ec59d695..3c7f029b 100644
--- a/tokio/src/sync/mutex.rs
+++ b/tokio/src/sync/mutex.rs
@@ -73,25 +73,17 @@ unsafe impl<T> Send for Mutex<T> where T: Send {}
unsafe impl<T> Sync for Mutex<T> where T: Send {}
unsafe impl<'a, T> Sync for MutexGuard<'a, T> where T: Send + Sync {}
-/// An enumeration of possible errors associated with a `TryLockResult`
-/// which can occur while trying to acquire a lock from the `try_lock`
-/// method on a `Mutex`.
+/// Error returned from the [`Mutex::try_lock`] function.
+///
+/// A `try_lock` operation can only fail if the mutex is already locked.
+///
+/// [`Mutex::try_lock`]: Mutex::try_lock
#[derive(Debug)]
-pub enum TryLockError {
- /// The lock could not be acquired at this time because the operation
- /// would otherwise block.
- WouldBlock,
-}
+pub struct TryLockError(());
impl fmt::Display for TryLockError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(
- fmt,
- "{}",
- match self {
- TryLockError::WouldBlock => "operation would block"
- }
- )
+ write!(fmt, "{}", "operation would block")
}
}
@@ -134,7 +126,7 @@ impl<T> Mutex<T> {
let mut permit = semaphore::Permit::new();
match permit.try_acquire(&self.s) {
Ok(_) => Ok(MutexGuard { lock: self, permit }),
- Err(_) => Err(TryLockError::WouldBlock),
+ Err(_) => Err(TryLockError(())),
}
}
}