X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=library%2Fstd%2Fsrc%2Ferror.rs;h=df7a49d258282488ff8e1e8e54d13da68bdb0f61;hb=20f124a1934cf833467ccfbe6c31daff160fec56;hp=ff7b70c328daf6b62e29e7b3b1aa5b34a4c3ddb2;hpb=7a782537b17a0cf8f46e9eb67d818275941cc0bc;p=rust.git diff --git a/library/std/src/error.rs b/library/std/src/error.rs index ff7b70c328d..df7a49d2582 100644 --- a/library/std/src/error.rs +++ b/library/std/src/error.rs @@ -260,20 +260,6 @@ fn type_id(&self, _: private::Internal) -> TypeId TypeId::of::() } - /// Returns a stack backtrace, if available, of where this error occurred. - /// - /// This function allows inspecting the location, in code, of where an error - /// happened. The returned `Backtrace` contains information about the stack - /// trace of the OS thread of execution of where the error originated from. - /// - /// Note that not all errors contain a `Backtrace`. Also note that a - /// `Backtrace` may actually be empty. For more information consult the - /// `Backtrace` type itself. - #[unstable(feature = "backtrace", issue = "53487")] - fn backtrace(&self) -> Option<&Backtrace> { - None - } - /// ``` /// if let Err(e) = "xc".parse::() { /// // Print `e` itself, no need for description(). @@ -370,7 +356,7 @@ fn provide<'a>(&'a self, req: &mut Demand<'a>) {} } #[unstable(feature = "error_generic_member_access", issue = "99301")] -impl Provider for dyn Error + 'static { +impl<'b> Provider for dyn Error + 'b { fn provide<'a>(&'a self, req: &mut Demand<'a>) { self.provide(req) } @@ -757,8 +743,8 @@ fn source(&self) -> Option<&(dyn Error + 'static)> { Error::source(&**self) } - fn backtrace(&self) -> Option<&Backtrace> { - Error::backtrace(&**self) + fn provide<'b>(&'b self, req: &mut Demand<'b>) { + Error::provide(&**self, req); } } @@ -778,8 +764,8 @@ fn source(&self) -> Option<&(dyn Error + 'static)> { Error::source(&**self) } - fn backtrace(&self) -> Option<&Backtrace> { - Error::backtrace(&**self) + fn provide<'a>(&'a self, req: &mut Demand<'a>) { + Error::provide(&**self, req); } } @@ -871,6 +857,20 @@ fn source(&self) -> Option<&(dyn Error + 'static)> { } } +impl<'a> dyn Error + 'a { + /// Request a reference of type `T` as context about this error. + #[unstable(feature = "error_generic_member_access", issue = "99301")] + pub fn request_ref(&'a self) -> Option<&'a T> { + core::any::request_ref(self) + } + + /// Request a value of type `T` as context about this error. + #[unstable(feature = "error_generic_member_access", issue = "99301")] + pub fn request_value(&'a self) -> Option { + core::any::request_value(self) + } +} + // Copied from `any.rs`. impl dyn Error + 'static { /// Returns `true` if the inner type is the same as `T`. @@ -910,18 +910,6 @@ pub fn downcast_mut(&mut self) -> Option<&mut T> { None } } - - /// Request a reference of type `T` as context about this error. - #[unstable(feature = "error_generic_member_access", issue = "99301")] - pub fn request_ref(&self) -> Option<&T> { - core::any::request_ref(self) - } - - /// Request a value of type `T` as context about this error. - #[unstable(feature = "error_generic_member_access", issue = "99301")] - pub fn request_value(&self) -> Option { - core::any::request_value(self) - } } impl dyn Error + 'static + Send { @@ -949,13 +937,13 @@ pub fn downcast_mut(&mut self) -> Option<&mut T> { /// Request a reference of type `T` as context about this error. #[unstable(feature = "error_generic_member_access", issue = "99301")] pub fn request_ref(&self) -> Option<&T> { - ::request_ref(self) + ::request_ref(self) } /// Request a value of type `T` as context about this error. #[unstable(feature = "error_generic_member_access", issue = "99301")] pub fn request_value(&self) -> Option { - ::request_value(self) + ::request_value(self) } } @@ -984,13 +972,13 @@ pub fn downcast_mut(&mut self) -> Option<&mut T> { /// Request a reference of type `T` as context about this error. #[unstable(feature = "error_generic_member_access", issue = "99301")] pub fn request_ref(&self) -> Option<&T> { - ::request_ref(self) + ::request_ref(self) } /// Request a value of type `T` as context about this error. #[unstable(feature = "error_generic_member_access", issue = "99301")] pub fn request_value(&self) -> Option { - ::request_value(self) + ::request_value(self) } } @@ -1466,9 +1454,11 @@ pub fn pretty(mut self, pretty: bool) -> Self { /// /// ```rust /// #![feature(error_reporter)] - /// #![feature(backtrace)] + /// #![feature(provide_any)] + /// #![feature(error_generic_member_access)] /// # use std::error::Error; /// # use std::fmt; + /// use std::any::Demand; /// use std::error::Report; /// use std::backtrace::Backtrace; /// @@ -1498,8 +1488,9 @@ pub fn pretty(mut self, pretty: bool) -> Self { /// } /// /// impl Error for SuperErrorSideKick { - /// fn backtrace(&self) -> Option<&Backtrace> { - /// Some(&self.backtrace) + /// fn provide<'a>(&'a self, req: &mut Demand<'a>) { + /// req + /// .provide_ref::(&self.backtrace); /// } /// } /// @@ -1552,11 +1543,11 @@ impl Report fn backtrace(&self) -> Option<&Backtrace> { // have to grab the backtrace on the first error directly since that error may not be // 'static - let backtrace = self.error.backtrace(); + let backtrace = (&self.error as &dyn Error).request_ref(); let backtrace = backtrace.or_else(|| { self.error .source() - .map(|source| source.chain().find_map(|source| source.backtrace())) + .map(|source| source.chain().find_map(|source| source.request_ref())) .flatten() }); backtrace @@ -1618,11 +1609,11 @@ impl Report> { fn backtrace(&self) -> Option<&Backtrace> { // have to grab the backtrace on the first error directly since that error may not be // 'static - let backtrace = self.error.backtrace(); + let backtrace = self.error.request_ref(); let backtrace = backtrace.or_else(|| { self.error .source() - .map(|source| source.chain().find_map(|source| source.backtrace())) + .map(|source| source.chain().find_map(|source| source.request_ref())) .flatten() }); backtrace