Skip to main content

qubit_lock/lock/
try_lock_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Try Lock Error
11//!
12//! Error type for non-blocking lock acquisition.
13//!
14
15use std::fmt;
16
17/// Non-blocking lock acquisition error.
18///
19/// This error type is used by `try_read` and `try_write` to distinguish
20/// immediate lock contention from poisoned lock states. The
21/// [`Self::Poisoned`] variant is returned only by lock implementations that
22/// support poisoning.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum TryLockError {
25    /// The lock could not be acquired immediately because another guard is active.
26    WouldBlock,
27    /// The lock implementation reports a poisoned state.
28    Poisoned,
29}
30
31impl fmt::Display for TryLockError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::WouldBlock => f.write_str("lock acquisition would block"),
35            Self::Poisoned => f.write_str("lock is poisoned"),
36        }
37    }
38}
39
40impl std::error::Error for TryLockError {}