bitcode/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#[cfg(debug_assertions)]
use alloc::borrow::Cow;
use core::fmt::{Debug, Display, Formatter};

/// Short version of `Err(error("..."))`.
pub fn err<T>(msg: &'static str) -> Result<T, Error> {
    Err(error(msg))
}

/// Creates an error with a message that might be displayed.
pub fn error(_msg: &'static str) -> Error {
    #[cfg(debug_assertions)]
    return Error(Cow::Borrowed(_msg));
    #[cfg(not(debug_assertions))]
    Error(())
}

/// Creates an error from a `T:` [`Display`].
#[cfg(feature = "serde")]
pub fn error_from_display(_t: impl Display) -> Error {
    #[cfg(debug_assertions)]
    use alloc::string::ToString;
    #[cfg(debug_assertions)]
    return Error(Cow::Owned(_t.to_string()));
    #[cfg(not(debug_assertions))]
    Error(())
}

#[cfg(debug_assertions)]
type ErrorImpl = Cow<'static, str>;
#[cfg(not(debug_assertions))]
type ErrorImpl = ();

/// Decoding / (De)serialization errors.
/// # Debug mode
/// In debug mode, the error contains a reason.
/// # Release mode
/// In release mode, the error is a zero-sized type for efficiency.
#[cfg_attr(test, derive(PartialEq))]
pub struct Error(ErrorImpl);
impl Debug for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        #[cfg(debug_assertions)]
        return write!(f, "Error({:?})", self.0);
        #[cfg(not(debug_assertions))]
        f.write_str("Error(\"bitcode error\")")
    }
}
impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        #[cfg(debug_assertions)]
        return f.write_str(&self.0);
        #[cfg(not(debug_assertions))]
        f.write_str("bitcode error")
    }
}
#[cfg(feature = "std")]
// TODO expose to no_std when error_in_core stabilized (https://github.com/rust-lang/rust/issues/103765)
impl std::error::Error for Error {}