KCOCFOS6KMCTVS6K3WE6KOQSWC3QNWN2E36ASHQPP3Z3H4BHR3NQC
GREPEM32FUEZFD5PNO5RLCGA6DJBFBFXZR4IQ6RQN6YRQ4QMBEHAC
LX7EZ6TKJCQI3K7SV56WMTPFY672WN5UOBGXKKXQI7S73GMWNNXAC
5JPOXYSROPEEERSG5D4XCBZTB6YBJRC767UCQOOD4KS72R56O7AAC
P74ZROEVCWORKPSBYDCNPSSZFTI7AXJAHWNFOMWFWNSE7OMYNUBAC
ZDN7BJ3JA3VL4AYWX3SV24GHP6NCAETJVAKAXKMIZTLHTWLUNMOQC
3LI5FAF5ULXOWBMTG4XCUOMUPVROSFDY6FWGE574OH4JGPMOPNVAC
CITEDKPB6MKVZUEYEDE5ZKTNVY35HCOAXKDPYG7YLLEOVFNMSRXQC
RQSDP2B2OVRFRJ33OZCVB4TYCQMSR3JZYG7FNA6TQ3HFABTGOGMQC
UR5XDZ3SS7QXSUFAYQDYUXX4QDROIYVXUH3Z24F6PAWJDNODAZYAC
}
}
/// ```
/// # type T = ();
/// #
/// use anyhow::{Context, Result};
///
/// fn maybe_get() -> Option<T> {
/// # const IGNORE: &str = stringify! {
/// ...
/// # };
/// # unimplemented!()
/// }
///
/// fn demo() -> Result<()> {
/// let t = maybe_get().context("there is no T")?;
/// # const IGNORE: &str = stringify! {
/// ...
/// # };
/// # unimplemented!()
/// }
/// ```
impl<T> Context<T, Infallible> for Option<T> {
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
{
self.ok_or_else(|| Error::from_display(context, backtrace!()))
// same layout as the typeid specifies.
unsafe { Error::construct(error, type_id, backtrace) }
}
pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
where
M: Display + Send + Sync + 'static,
{
let error = DisplayError(message);
let type_id = TypeId::of::<M>();
// Safety: DisplayError is repr(transparent) so DisplayError<M> has the
#[repr(transparent)]
struct DisplayError<M>(M);
impl<M> Debug for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> Display for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> StdError for DisplayError<M> where M: Display + 'static {}