Skip to content
Myr

Import std::error::Error as StdError

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

To avoid name collisions when renaming Exception to Error.

authored by David Tolnay · committed by David Tolnay · parent 2000930 · Browse files · 3 files+31 added−31 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+31 added−31 removed

  • other:+31 added−31 removed

3 files+31 added−31 removed

modified:src/as_error.rs+7 added-7 removed
1use std::error::Error;1use std::error::Error as StdError;23use crate::Exception;45/// View type as an error object.6pub trait AsError {7 /// View type as an error object.8 fn as_error(&self) -> &(dyn Error + Send + Sync + 'static);8 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static);9}1011impl<T: Error + Send + Sync + 'static> AsError for T {12 fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {11impl<T: StdError + Send + Sync + 'static> AsError for T {12 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {13 self14 }15}1617impl AsError for dyn Error + Send + Sync + 'static {18 fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {17impl AsError for dyn StdError + Send + Sync + 'static {18 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {19 self20 }21}2223impl AsError for Exception {24 fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {24 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {25 &**self26 }27}
modified:src/context.rs+8 added-8 removed
1use std::backtrace::Backtrace;2use std::error::Error;2use std::error::Error as StdError;3use std::fmt::{self, Debug, Display};45use crate::Exception;6
8 unmodified lines
15 C: Display + Send + Sync + 'static,16 F: FnOnce(&E) -> C;17}1819impl<T, E: Error + Send + Sync + 'static> Context<T, E> for Result<T, E> {19impl<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, Exception> {21 self.map_err(|error| Exception::from(ContextError { error, context }))22 }23
46 unmodified lines
70 Display::fmt(&self.context, f)71 }72}7374impl<E: Error + 'static, C: Display> Error for ContextError<E, C> {74impl<E: StdError + 'static, C: Display> StdError for ContextError<E, C> {75 fn backtrace(&self) -> Option<&Backtrace> {76 self.error.backtrace()77 }7879 fn cause(&self) -> Option<&dyn Error> {79 fn cause(&self) -> Option<&dyn StdError> {80 Some(&self.error)81 }8283 fn source(&self) -> Option<&(dyn Error + 'static)> {83 fn source(&self) -> Option<&(dyn StdError + 'static)> {84 Some(&self.error)85 }86}8788impl<C: Display> Error for ContextError<Exception, C> {88impl<C: Display> StdError for ContextError<Exception, C> {89 fn backtrace(&self) -> Option<&Backtrace> {90 Some(self.error.backtrace())91 }9293 fn cause(&self) -> Option<&dyn Error> {93 fn cause(&self) -> Option<&dyn StdError> {94 Some(&*self.error)95 }9697 fn source(&self) -> Option<&(dyn Error + 'static)> {97 fn source(&self) -> Option<&(dyn StdError + 'static)> {98 Some(&*self.error)99 }100}
modified:src/error.rs+16 added-16 removed
1use std::any::TypeId;2use std::backtrace::{Backtrace, BacktraceStatus};3use std::error::Error;3use std::error::Error as StdError;4use std::fmt::{self, Debug, Display};5use std::mem;6use std::ops::{Deref, DerefMut};7use std::ptr;
19 unmodified lines
27 /// If the error type does not provide a backtrace, a backtrace will be created here to ensure28 /// that a backtrace exists.29 pub fn new<E>(error: E) -> Exception30 where31 E: Error + Send + Sync + 'static,31 E: StdError + Send + Sync + 'static,32 {33 Exception::construct(error, TypeId::of::<E>())34 }35
6 unmodified lines
42 }4344 fn construct<E>(error: E, type_id: TypeId) -> Exception45 where46 E: Error + Send + Sync + 'static,46 E: StdError + Send + Sync + 'static,47 {48 unsafe {49 let backtrace = match error.backtrace() {50 Some(_) => None,51 None => Some(Backtrace::capture()),52 };53 let obj: TraitObject = mem::transmute(&error as &dyn Error);53 let obj: TraitObject = mem::transmute(&error as &dyn StdError);54 let vtable = obj.vtable;55 let inner = InnerException {56 vtable,57 type_id,
6 unmodified lines
64 }65 }6667 /// View this exception as the underlying error.68 pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {68 pub fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {69 &**self70 }7172 /// View this exception as the underlying error, mutably.73 pub fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {73 pub fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {74 &mut **self75 }7677 /// Get the backtrace for this Exception.
38 unmodified lines
116117 /// Downcast this exception by reference.118 pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(&self) -> Option<&E> {119 if self.is::<E>() {120 unsafe { Some(&*(self.inner.error() as *const dyn Error as *const E)) }120 unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }121 } else {122 None123 }124 }125126 /// Downcast this exception by mutable reference.127 pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(&mut self) -> Option<&mut E> {128 if self.is::<E>() {129 unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn Error as *mut E)) }129 unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn StdError as *mut E)) }130 } else {131 None132 }133 }134}135136impl<E: Error + Send + Sync + 'static> From<E> for Exception {136impl<E: StdError + Send + Sync + 'static> From<E> for Exception {137 fn from(error: E) -> Exception {138 Exception::new(error)139 }140}141142impl Deref for Exception {143 type Target = dyn Error + Send + Sync + 'static;143 type Target = dyn StdError + Send + Sync + 'static;144 fn deref(&self) -> &Self::Target {145 self.inner.error()146 }147}
83 unmodified lines
231 Display::fmt(&self.0, f)232 }233}234235impl<M: Display + Debug + 'static> Error for MessageError<M> {}235impl<M: Display + Debug + 'static> StdError for MessageError<M> {}236237impl InnerException<()> {238 fn error(&self) -> &(dyn Error + Send + Sync + 'static) {238 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {239 unsafe {240 mem::transmute(TraitObject {241 data: &self.error,242 vtable: self.vtable,243 })244 }245 }246247 fn error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {247 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {248 unsafe {249 mem::transmute(TraitObject {250 data: &mut self.error,251 vtable: self.vtable,
3 unmodified lines
255}256257/// Iterator of errors in an `Exception`.258pub struct Errors<'a> {259 next: Option<&'a (dyn Error + 'static)>,259 next: Option<&'a (dyn StdError + 'static)>,260}261262impl<'a> Iterator for Errors<'a> {263 type Item = &'a (dyn Error + 'static);263 type Item = &'a (dyn StdError + 'static);264265 fn next(&mut self) -> Option<Self::Item> {266 let next = self.next.take()?;267 self.next = next.source();
25 unmodified lines
293 use std::sync::*;294295 #[derive(Debug)]296 struct HasDrop(Box<Arc<Mutex<bool>>>);297 impl Error for HasDrop {}297 impl StdError for HasDrop {}298 impl Display for HasDrop {299 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {300 write!(f, "does something")301 }
16 unmodified lines