Skip to main content

ipc_lock/
error.rs

1use std::fmt;
2use std::io;
3
4/// Specialized `Result` for this crate.
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Errors that can occur when using [`Lock`](crate::Lock).
8#[derive(Debug)]
9pub enum Error {
10    /// The lock name is invalid.
11    ///
12    /// Names must be non-empty and must not contain `\0`, `/`, or `\`.
13    InvalidName,
14
15    /// The lock is currently held by another thread or process and a
16    /// non-blocking acquire was requested.
17    WouldBlock,
18
19    /// An underlying I/O error occurred.
20    Io(io::Error),
21}
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Error::InvalidName => f.write_str(
27                "invalid lock name: must be non-empty and contain no '\\0', '/', or '\\'",
28            ),
29            Error::WouldBlock => f.write_str("lock is currently held by another thread or process"),
30            Error::Io(e) => write!(f, "I/O error: {e}"),
31        }
32    }
33}
34
35impl std::error::Error for Error {
36    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37        match self {
38            Error::Io(e) => Some(e),
39            _ => None,
40        }
41    }
42}
43
44impl From<io::Error> for Error {
45    fn from(e: io::Error) -> Self {
46        Error::Io(e)
47    }
48}