lockpool/
error.rs

1use std::fmt::Debug;
2use thiserror::Error;
3
4/// A type of error which can be returned whenever a lock is acquired.
5///
6/// A lock is poisoned whenever a thread panics while a lock is held.
7#[derive(Error, Debug, PartialEq, Eq, Hash, Clone, Copy)]
8#[error("The lock with the key `{key:?}` couldn't be acquired because another thread panicked while holding this lock")]
9pub struct PoisonError<K, G> {
10    /// The key of the lock that was attempted to become locked
11    pub key: K,
12    pub(super) guard: G,
13}
14
15/// Errors that can be thrown by [LockPool::unpoison](super::LockPool::unpoison).
16#[derive(Error, Debug, PartialEq, Eq, Hash, Clone, Copy)]
17pub enum UnpoisonError {
18    /// Tried to unpoison a lock that wasn't poisoned
19    #[error("Tried to unpoison a lock that wasn't poisoned")]
20    NotPoisoned,
21
22    /// At least one other thread is currently blocked on this mutex, we cannot unpoison it
23    #[error("At least one other thread is currently blocked on this mutex, we cannot unpoison it")]
24    OtherThreadsBlockedOnMutex,
25}
26
27/// Errors that can be thrown by [LockPool::try_lock](super::LockPool::try_lock).
28#[derive(Error, Debug, PartialEq, Eq, Hash, Clone, Copy)]
29pub enum TryLockError<K, G> {
30    /// The lock is poisoned, see [PoisonError]
31    #[error(transparent)]
32    Poisoned(PoisonError<K, G>),
33
34    /// The lock could not be acquired at this time because the operation would otherwise block
35    #[error(
36        "The lock could not be acquired at this time because the operation would otherwise block"
37    )]
38    WouldBlock,
39}