token_ref_cell/
error.rs

1//! Errors.
2
3use core::fmt;
4
5/// Error returned by [`TokenRefCell::try_borrow`](crate::TokenRefCell::try_borrow).
6#[derive(Debug)]
7pub struct BorrowError;
8
9impl BorrowError {
10    // This ensures the panicking code is outlined from inlined functions
11    pub(crate) fn panic(self) -> ! {
12        panic!("{self}")
13    }
14}
15
16impl fmt::Display for BorrowError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        write!(f, "wrong token")
19    }
20}
21
22#[cfg(feature = "std")]
23impl std::error::Error for BorrowError {}
24
25#[allow(missing_docs)]
26/// Error returned by [`TokenRefCell::try_borrow_mut`](crate::TokenRefCell::try_borrow_mut).
27#[derive(Debug)]
28pub enum BorrowMutError {
29    WrongToken,
30    NotUniqueToken,
31}
32
33impl BorrowMutError {
34    // This ensures the panicking code is outlined from inlined functions
35    pub(crate) fn panic(self) -> ! {
36        panic!("{self}")
37    }
38}
39
40impl fmt::Display for BorrowMutError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::WrongToken => write!(f, "wrong token"),
44            Self::NotUniqueToken => write!(f, "not unique token"),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl std::error::Error for BorrowMutError {}
51
52/// Error returned when unique token has already been initialized.
53#[derive(Debug)]
54pub struct AlreadyInitialized;
55
56impl fmt::Display for AlreadyInitialized {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(f, "already initialized")
59    }
60}
61
62#[cfg(feature = "std")]
63impl std::error::Error for AlreadyInitialized {}