Move error formatting to fmt.rs
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 b1c7c7a · Browse files · 3 files·+70 added−65 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+70 added−65 removed
- other:+70 added−65 removed
Provenance
- Commit
7da2a83- Tree
20c5eee- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
b1c7c7a
3 files changed+70 added−65 removed
modified:src/error.rs+4 added-65 removed
650 unmodified lines651 }652 }653 654 impl ErrorImpl<()> {655- fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {655+ pub(crate) fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {656 unsafe { &*(self.vtable.object_ref)(self) }657 }658 659- fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {659+ pub(crate) fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {660 unsafe { &mut *(self.vtable.object_mut)(self) }661 }662 663 #[cfg(backtrace)]664- fn backtrace(&self) -> &Backtrace {664+ pub(crate) fn backtrace(&self) -> &Backtrace {665 // This unwrap can only panic if the underlying error's backtrace method666 // is nondeterministic, which would only happen in maliciously667 // constructed code.668 self.backtrace1 unmodified line670 .or_else(|| self.error().backtrace())671 .expect("backtrace capture failed")672 }673 674- fn chain(&self) -> Chain {674+ pub(crate) fn chain(&self) -> Chain {675 Chain {676 next: Some(self.error()),677 }678 }679-680- fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result {681- write!(f, "{}", self.error())?;682-683- let mut chain = self.chain().skip(1).enumerate().peekable();684- if let Some((n, error)) = chain.next() {685- write!(f, "\n\nCaused by:\n ")?;686- if chain.peek().is_some() {687- write!(f, "{}: ", n)?;688- }689- write!(f, "{}", error)?;690- for (n, error) in chain {691- write!(f, "\n {}: {}", n, error)?;692- }693- }694-695- Ok(())696- }697-698- fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {699- if f.alternate() {700- self.print_chain(f)701- } else {702- write!(f, "{}", self.error())703- }704- }705-706- fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {707- if f.alternate() {708- return Debug::fmt(self.error(), f);709- }710-711- self.print_chain(f)?;712- writeln!(f)?;713-714- #[cfg(backtrace)]715- {716- use std::backtrace::BacktraceStatus;717-718- let backtrace = self.backtrace();719- match backtrace.status() {720- BacktraceStatus::Captured => {721- let mut backtrace = backtrace.to_string();722- if backtrace.starts_with("stack backtrace:") {723- // Capitalize to match "Caused by:"724- backtrace.replace_range(0..1, "S");725- }726- write!(f, "\n{}", backtrace)?;727- }728- BacktraceStatus::Disabled => {729- writeln!(730- f,731- "\nStack backtrace:\n Run with RUST_LIB_BACKTRACE=1 env variable to display a backtrace"732- )?;733- }734- _ => {}735- }736- }737-738- Ok(())739- }679 }680 681 impl<E> StdError for ErrorImpl<E>682 where56 unmodified linesadded:src/fmt.rs+65 added
1+use crate::error::ErrorImpl;2+use std::fmt::{self, Debug};3+4+impl ErrorImpl<()> {5+ fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result {6+ write!(f, "{}", self.error())?;7+8+ let mut chain = self.chain().skip(1).enumerate().peekable();9+ if let Some((n, error)) = chain.next() {10+ write!(f, "\n\nCaused by:\n ")?;11+ if chain.peek().is_some() {12+ write!(f, "{}: ", n)?;13+ }14+ write!(f, "{}", error)?;15+ for (n, error) in chain {16+ write!(f, "\n {}: {}", n, error)?;17+ }18+ }19+20+ Ok(())21+ }22+23+ pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {24+ if f.alternate() {25+ self.print_chain(f)26+ } else {27+ write!(f, "{}", self.error())28+ }29+ }30+31+ pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {32+ if f.alternate() {33+ return Debug::fmt(self.error(), f);34+ }35+36+ self.print_chain(f)?;37+ writeln!(f)?;38+39+ #[cfg(backtrace)]40+ {41+ use std::backtrace::BacktraceStatus;42+43+ let backtrace = self.backtrace();44+ match backtrace.status() {45+ BacktraceStatus::Captured => {46+ let mut backtrace = backtrace.to_string();47+ if backtrace.starts_with("stack backtrace:") {48+ // Capitalize to match "Caused by:"49+ backtrace.replace_range(0..1, "S");50+ }51+ write!(f, "\n{}", backtrace)?;52+ }53+ BacktraceStatus::Disabled => {54+ writeln!(55+ f,56+ "\nStack backtrace:\n Run with RUST_LIB_BACKTRACE=1 env variable to display a backtrace"57+ )?;58+ }59+ _ => {}60+ }61+ }62+63+ Ok(())64+ }65+}modified:src/lib.rs+1 added
169 unmodified lines170 #[macro_use]171 mod backtrace;172 mod context;173 mod error;174+mod fmt;175 mod kind;176 mod macros;177 178 #[cfg(not(feature = "std"))]271 unmodified lines