Skip to content
Myr

Support context on Option

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent c06155e · Browse files · 3 files+74 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+74 added−1 removed

  • other:+74 added−1 removed

3 files+74 added−1 removed

modified:src/context.rs+38 added
1use crate::Error;2use std::convert::Infallible;3use std::error::Error as StdError;4use std::fmt::{self, Debug, Display};56#[cfg(backtrace)]
96 unmodified lines
103 self.map_err(|error| error.context(context()))104 }105}106107/// ```108/// # type T = ();109/// #110/// use anyhow::{Context, Result};111///112/// fn maybe_get() -> Option<T> {113/// # const IGNORE: &str = stringify! {114/// ...115/// # };116/// # unimplemented!()117/// }118///119/// fn demo() -> Result<()> {120/// let t = maybe_get().context("there is no T")?;121/// # const IGNORE: &str = stringify! {122/// ...123/// # };124/// # unimplemented!()125/// }126/// ```127impl<T> Context<T, Infallible> for Option<T> {128 fn context<C>(self, context: C) -> Result<T, Error>129 where130 C: Display + Send + Sync + 'static,131 {132 self.ok_or_else(|| Error::from_display(context, backtrace!()))133 }134135 fn with_context<C, F>(self, context: F) -> Result<T, Error>136 where137 C: Display + Send + Sync + 'static,138 F: FnOnce() -> C,139 {140 self.ok_or_else(|| Error::from_display(context(), backtrace!()))141 }142}143144pub(crate) struct ContextError<E, C> {145 pub error: E,146 pub context: C,147}
48 unmodified lines
modified:src/error.rs+35 added
57 unmodified lines
58 // same layout as the typeid specifies.59 unsafe { Error::construct(error, type_id, backtrace) }60 }6162 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self63 where64 M: Display + Send + Sync + 'static,65 {66 let error = DisplayError(message);67 let type_id = TypeId::of::<M>();6869 // Safety: DisplayError is repr(transparent) so DisplayError<M> has the70 // same layout as the typeid specifies.71 unsafe { Error::construct(error, type_id, backtrace) }72 }7374 // Takes backtrace as argument rather than capturing it here so that the75 // user sees one fewer layer of wrapping noise in the backtrace.76 //77 // Unsafe because the type represented by type_id must have the same layout
357 unmodified lines
435}436437impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}438439#[repr(transparent)]440struct DisplayError<M>(M);441442impl<M> Debug for DisplayError<M>443where444 M: Display,445{446 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {447 Display::fmt(&self.0, f)448 }449}450451impl<M> Display for DisplayError<M>452where453 M: Display,454{455 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {456 Display::fmt(&self.0, f)457 }458}459460impl<M> StdError for DisplayError<M> where M: Display + 'static {}461462impl ErrorImpl<()> {463 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {464 unsafe { &*(self.vtable.object)(&self.error) }465 }
90 unmodified lines
modified:.travis.yml+1 added-1 removed