#[cfg(debug_assertions)]
use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter};
pub fn err<T>(msg: &'static str) -> Result<T, Error> {
Err(error(msg))
}
pub fn error(_msg: &'static str) -> Error {
#[cfg(debug_assertions)]
return Error(Cow::Borrowed(_msg));
#[cfg(not(debug_assertions))]
Error(())
}
#[cfg(feature = "serde")]
pub fn error_from_display(_t: impl Display) -> Error {
#[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 = ();
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Error(ErrorImpl);
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
#[cfg(debug_assertions)]
return f.write_str(&self.0);
#[cfg(not(debug_assertions))]
f.write_str("bitcode error")
}
}
impl std::error::Error for Error {}