Consolidate to one Context impl for Result to improve inference
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 6057864 · Browse files · 1 file·+33 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+33 added−23 removed
- other:+33 added−23 removed
Provenance
- Commit
c695e63- Tree
6a36520- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
6057864
1 file changed+33 added−23 removed
modified:src/context.rs+33 added-23 removed
63 unmodified lines64 C: Display + Send + Sync + 'static,65 F: FnOnce() -> C;66 }67 68-impl<T, E> Context<T, E> for Result<T, E>69-where70- E: StdError + Send + Sync + 'static,71-{72- fn context<C>(self, context: C) -> Result<T, Error>73- where74- C: Display + Send + Sync + 'static,75- {76- self.map_err(|error| Error::new(ContextError { error, context }))68+mod ext {69+ use super::*;70+71+ pub trait StdError {72+ fn ext_context<C>(self, context: C) -> Error73+ where74+ C: Display + Send + Sync + 'static;75 }76 79- fn with_context<C, F>(self, context: F) -> Result<T, Error>77+ impl<E> StdError for E78 where81- C: Display + Send + Sync + 'static,82- F: FnOnce() -> C,79+ E: std::error::Error + Send + Sync + 'static,80 {84- self.map_err(|error| {81+ fn ext_context<C>(self, context: C) -> Error82+ where83+ C: Display + Send + Sync + 'static,84+ {85 Error::new(ContextError {86- error,87- context: context(),86+ error: self,87+ context,88 })89- })89+ }90 }91+92+ impl StdError for Error {93+ fn ext_context<C>(self, context: C) -> Error94+ where95+ C: Display + Send + Sync + 'static,96+ {97+ self.context(context)98+ }99+ }100 }101 93-impl<T> Context<T, Error> for Result<T, Error> {102+impl<T, E> Context<T, E> for Result<T, E>103+where104+ E: ext::StdError + Send + Sync + 'static,105+{106 fn context<C>(self, context: C) -> Result<T, Error>107 where108 C: Display + Send + Sync + 'static,109 {98- self.map_err(|error| error.context(context))110+ self.map_err(|error| error.ext_context(context))111 }112 113 fn with_context<C, F>(self, context: F) -> Result<T, Error>114 where115 C: Display + Send + Sync + 'static,116 F: FnOnce() -> C,117 {106- self.map_err(|error| error.context(context()))118+ self.map_err(|error| error.ext_context(context()))119 }120 }121 122 /// ```86 unmodified lines209 }210 }211 212 mod private {201- use crate::Error;202- use std::error::Error as StdError;213+ use super::*;214 215 pub trait Sealed {}216 206- impl<T, E> Sealed for Result<T, E> where E: StdError + Send + Sync + 'static {}207- impl<T> Sealed for Result<T, Error> {}217+ impl<T, E> Sealed for Result<T, E> where E: ext::StdError {}218 impl<T> Sealed for Option<T> {}219 }