Skip to content
Myr

Move trait bounds to where-clauses

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

I find it more readable how this appears in rustdoc.

authored by David Tolnay · committed by David Tolnay · parent 8da8c97 · Browse files · 3 files+69 added−18 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+69 added−18 removed

  • other:+69 added−18 removed

3 files+69 added−18 removed

modified:src/as_error.rs+4 added-1 removed
6 unmodified lines
7 /// View type as an error object.8 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static);9}1011impl<T: StdError + Send + Sync + 'static> AsError for T {11impl<T> AsError for T12where13 T: StdError + Send + Sync + 'static,14{15 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {16 self17 }18}
12 unmodified lines
modified:src/context.rs+33 added-8 removed
5 unmodified lines
67/// Provides the `context` method for `Result`.8pub trait Context<T, E> {9 /// Wrap the error value with additional context.10 fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error>;10 fn context<C>(self, context: C) -> Result<T, Error>11 where12 C: Display + Send + Sync + 'static;1314 /// Wrap the error value with additional context lazily.15 fn with_context<C, F>(self, f: F) -> Result<T, Error>16 where17 C: Display + Send + Sync + 'static,18 F: FnOnce(&E) -> C;19}2019impl<T, E: StdError + Send + Sync + 'static> Context<T, E> for Result<T, E> {20 fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {21impl<T, E> Context<T, E> for Result<T, E>22where23 E: StdError + Send + Sync + 'static,24{25 fn context<C>(self, context: C) -> Result<T, Error>26 where27 C: Display + Send + Sync + 'static,28 {29 self.map_err(|error| Error::from(ContextError { error, context }))30 }3132 fn with_context<C, F>(self, f: F) -> Result<T, Error>
10 unmodified lines
43 }44}4546impl<T> Context<T, Error> for Result<T, Error> {39 fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {47 fn context<C>(self, context: C) -> Result<T, Error>48 where49 C: Display + Send + Sync + 'static,50 {51 self.map_err(|error| Error::from(ContextError { error, context }))52 }5354 fn with_context<C, F>(self, f: F) -> Result<T, Error>
14 unmodified lines
69 error: E,70 context: C,71}7262impl<E: Debug, C: Display> Debug for ContextError<E, C> {73impl<E, C> Debug for ContextError<E, C>74where75 E: Debug,76 C: Display,77{78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {79 write!(f, "{:?}\n\n{}", self.error, self.context)80 }81}8268impl<E, C: Display> Display for ContextError<E, C> {83impl<E, C> Display for ContextError<E, C>84where85 C: Display,86{87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {88 Display::fmt(&self.context, f)89 }90}9174impl<E: StdError + 'static, C: Display> StdError for ContextError<E, C> {92impl<E, C> StdError for ContextError<E, C>93where94 E: StdError + 'static,95 C: Display,96{97 fn backtrace(&self) -> Option<&Backtrace> {98 self.error.backtrace()99 }100
5 unmodified lines
106 Some(&self.error)107 }108}10988impl<C: Display> StdError for ContextError<Error, C> {110impl<C> StdError for ContextError<Error, C>111where112 C: Display,113{114 fn backtrace(&self) -> Option<&Backtrace> {115 Some(self.error.backtrace())116 }117
8 unmodified lines
modified:src/error.rs+32 added-9 removed
98 unmodified lines
99 }100 }101102 /// Returns `true` if `E` is the type wrapped by this error object.103 pub fn is<E: Display + Debug + Send + Sync + 'static>(&self) -> bool {103 pub fn is<E>(&self) -> bool104 where105 E: Display + Debug + Send + Sync + 'static,106 {107 TypeId::of::<E>() == self.inner.type_id108 }109110 /// Attempt to downcast the error object to a concrete type.108 pub fn downcast<E: Display + Debug + Send + Sync + 'static>(self) -> Result<E, Error> {111 pub fn downcast<E>(self) -> Result<E, Error>112 where113 E: Display + Debug + Send + Sync + 'static,114 {115 if let Some(error) = self.downcast_ref::<E>() {116 unsafe {117 let error = ptr::read(error);118 drop(ptr::read(&self.inner));
5 unmodified lines
124 }125 }126127 /// Downcast this error object by reference.122 pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(&self) -> Option<&E> {128 pub fn downcast_ref<E>(&self) -> Option<&E>129 where130 E: Display + Debug + Send + Sync + 'static,131 {132 if self.is::<E>() {133 unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }134 } else {135 None136 }137 }138139 /// Downcast this error object by mutable reference.131 pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(&mut self) -> Option<&mut E> {140 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>141 where142 E: Display + Debug + Send + Sync + 'static,143 {144 if self.is::<E>() {145 unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn StdError as *mut E)) }146 } else {147 None148 }149 }150}151140impl<E: StdError + Send + Sync + 'static> From<E> for Error {152impl<E> From<E> for Error153where154 E: StdError + Send + Sync + 'static,155{156 fn from(error: E) -> Error {157 Error::new(error)158 }159}
75 unmodified lines
235 vtable: *const (),236}237238#[repr(transparent)]224struct MessageError<M: Display + Debug>(M);239struct MessageError<M>(M)240where241 M: Display + Debug;242226impl<M: Display + Debug> Debug for MessageError<M> {243impl<M> Debug for MessageError<M>244where245 M: Display + Debug,246{247 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {248 Debug::fmt(&self.0, f)249 }250}251232impl<M: Display + Debug> Display for MessageError<M> {252impl<M> Display for MessageError<M>253where254 M: Display + Debug,255{256 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {257 Display::fmt(&self.0, f)258 }259}260238impl<M: Display + Debug + 'static> StdError for MessageError<M> {}261impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}262263impl ErrorImpl<()> {264 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {265 unsafe {
78 unmodified lines