From 865e05b5b47d3e27e8fe4458662e751e492a0f51 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 14 Jan 2021 14:55:13 +0300 Subject: Make printin the backtrace more convenient --- crates/stdx/src/lib.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 13aab1451..d9a62e943 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -25,6 +25,27 @@ pub fn timeit(label: &'static str) -> impl Drop { Guard { label, start: Instant::now() } } +/// Prints backtrace to stderr, useful for debugging. +#[cfg(feature = "backtrace")] +pub fn print_backtrace() { + let bt = backtrace::Backtrace::new(); + eprintln!("{:?}", bt); +} +#[cfg(not(feature = "backtrace"))] +pub fn print_backtrace() { + eprintln!( + r#"Enable the backtrace feature. +Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`. +"# + ); +} + +pub fn to_lower_snake_case(s: &str) -> String { + to_snake_case(s, char::to_ascii_lowercase) +} +pub fn to_upper_snake_case(s: &str) -> String { + to_snake_case(s, char::to_ascii_uppercase) +} fn to_snake_case char>(s: &str, change_case: F) -> String { let mut buf = String::with_capacity(s.len()); let mut prev = false; @@ -43,14 +64,6 @@ fn to_snake_case char>(s: &str, change_case: F) -> String { buf } -pub fn to_lower_snake_case(s: &str) -> String { - to_snake_case(s, char::to_ascii_lowercase) -} - -pub fn to_upper_snake_case(s: &str) -> String { - to_snake_case(s, char::to_ascii_uppercase) -} - pub fn replace(buf: &mut String, from: char, to: &str) { if !buf.contains(from) { return; -- cgit v1.2.3