Merge pull request #8 from dtolnay/option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Support context on Option
authored by David Tolnay · committed by GitHub · parents 42e5c93, aa2d080 · 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
Provenance
- Commit
240cc83- Tree
ab5dcf3- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
42e5c93- Parent 2
aa2d080
3 files changed+74 added−1 removed
modified:src/context.rs+38 added
1 use crate::Error;2+use std::convert::Infallible;3 use std::error::Error as StdError;4 use std::fmt::{self, Debug, Display};5 6 #[cfg(backtrace)]96 unmodified lines103 self.map_err(|error| error.context(context()))104 }105 }106 107+/// ```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+/// ```127+impl<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+ }134+135+ 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+}143+144 pub(crate) struct ContextError<E, C> {145 pub error: E,146 pub context: C,147 }48 unmodified linesmodified:src/error.rs+35 added
57 unmodified lines58 // same layout as the typeid specifies.59 unsafe { Error::construct(error, type_id, backtrace) }60 }61 62+ 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>();68+69+ // 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+ }73+74 // 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 layout357 unmodified lines435 }436 437 impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}438 439+#[repr(transparent)]440+struct DisplayError<M>(M);441+442+impl<M> Debug for DisplayError<M>443+where444+ M: Display,445+{446+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {447+ Display::fmt(&self.0, f)448+ }449+}450+451+impl<M> Display for DisplayError<M>452+where453+ M: Display,454+{455+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {456+ Display::fmt(&self.0, f)457+ }458+}459+460+impl<M> StdError for DisplayError<M> where M: Display + 'static {}461+462 impl ErrorImpl<()> {463 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {464 unsafe { &*(self.vtable.object)(&self.error) }465 }90 unmodified linesmodified:.travis.yml+1 added-1 removed
7 unmodified lines8910111212137 unmodified linesscript: cargo test
matrix: include: - rust: 1.32.0 - rust: 1.34.0 script: cargo check