fmt.rs
This file
Path
src/fmt.rs
158 lines · 4.1 KiB · Rust
Last changed by
Merge pull request #442 from dtolnay/backtraceDavid Tolnaybefore adoptione737fb6
History of this fileTake it
The walk stopped after 200 commits: lines older than 553ed38 point at it.
Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 1–7why, one step back1use crate::chain::Chain;2use crate::error::ErrorImpl;3use crate::ptr::Ref;4use core::fmt::{self, Debug, Write};56impl ErrorImpl {7 pub(crate) unsafe fn display(this: Ref<Self>, f: &mut fmt::Formatter) -> fmt::Result {Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49
line 8why, one step back8 write!(f, "{}", unsafe { Self::error(this) })?;Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 9–10why, one step back910 if f.alternate() {Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49
lines 11–12why, one step back11 let chain = unsafe { Self::chain(this) };12 for cause in chain.skip(1) {Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 13–20why, one step back13 write!(f, ": {}", cause)?;14 }15 }1617 Ok(())18 }1920 pub(crate) unsafe fn debug(this: Ref<Self>, f: &mut fmt::Formatter) -> fmt::Result {Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49
line 21why, one step back21 let error = unsafe { Self::error(this) };Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 22–42why, one step back2223 if f.alternate() {24 return Debug::fmt(error, f);25 }2627 write!(f, "{}", error)?;2829 if let Some(cause) = error.source() {30 write!(f, "\n\nCaused by:")?;31 let multiple = cause.source().is_some();32 for (n, error) in Chain::new(cause).enumerate() {33 writeln!(f)?;34 let mut indented = Indented {35 inner: f,36 number: if multiple { Some(n) } else { None },37 started: false,38 };39 write!(indented, "{}", error)?;40 }41 }42Merge pull request #440 from dtolnay/backtraceDavid Tolnaybefore adoption9b67e5d
line 43why, one step back43 #[cfg(feature = "std")]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
line 44why, one step back44 {Work around prelude redundant import warningsDavid Tolnaybefore adoptiondfc7bc0
line 45why, one step back45 use alloc::string::ToString;Merge pull request #442 from dtolnay/backtraceDavid Tolnaybefore adoptione737fb6
line 46why, one step back46 use std::backtrace::BacktraceStatus;Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
line 47why, one step back47Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49
line 48why, one step back48 let backtrace = unsafe { Self::backtrace(this) };Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 49–105why, one step back49 if let BacktraceStatus::Captured = backtrace.status() {50 let mut backtrace = backtrace.to_string();51 write!(f, "\n\n")?;52 if backtrace.starts_with("stack backtrace:") {53 // Capitalize to match "Caused by:"54 backtrace.replace_range(0..1, "S");55 } else {56 // "stack backtrace:" prefix was removed in57 // https://github.com/rust-lang/backtrace-rs/pull/28658 writeln!(f, "Stack backtrace:")?;59 }60 backtrace.truncate(backtrace.trim_end().len());61 write!(f, "{}", backtrace)?;62 }63 }6465 Ok(())66 }67}6869struct Indented<'a, D> {70 inner: &'a mut D,71 number: Option<usize>,72 started: bool,73}7475impl<T> Write for Indented<'_, T>76where77 T: Write,78{79 fn write_str(&mut self, s: &str) -> fmt::Result {80 for (i, line) in s.split('\n').enumerate() {81 if !self.started {82 self.started = true;83 match self.number {84 Some(number) => write!(self.inner, "{: >5}: ", number)?,85 None => self.inner.write_str(" ")?,86 }87 } else if i > 0 {88 self.inner.write_char('\n')?;89 if self.number.is_some() {90 self.inner.write_str(" ")?;91 } else {92 self.inner.write_str(" ")?;93 }94 }9596 self.inner.write_str(line)?;97 }9899 Ok(())100 }101}102103#[cfg(test)]104mod tests {105 use super::*;Work around prelude redundant import warningsDavid Tolnaybefore adoptiondfc7bc0
line 106why, one step back106 use alloc::string::String;Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 107–158why, one step back107108 #[test]109 fn one_digit() {110 let input = "verify\nthis";111 let expected = " 2: verify\n this";112 let mut output = String::new();113114 Indented {115 inner: &mut output,116 number: Some(2),117 started: false,118 }119 .write_str(input)120 .unwrap();121122 assert_eq!(expected, output);123 }124125 #[test]126 fn two_digits() {127 let input = "verify\nthis";128 let expected = " 12: verify\n this";129 let mut output = String::new();130131 Indented {132 inner: &mut output,133 number: Some(12),134 started: false,135 }136 .write_str(input)137 .unwrap();138139 assert_eq!(expected, output);140 }141142 #[test]143 fn no_digits() {144 let input = "verify\nthis";145 let expected = " verify\n this";146 let mut output = String::new();147148 Indented {149 inner: &mut output,150 number: None,151 started: false,152 }153 .write_str(input)154 .unwrap();155156 assert_eq!(expected, output);157 }158}