From 865e05b5b47d3e27e8fe4458662e751e492a0f51 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 14 Jan 2021 14:55:13 +0300 Subject: [PATCH] Make printin the backtrace more convenient --- Cargo.lock | 4 +++- crates/profile/Cargo.toml | 2 -- crates/profile/src/lib.rs | 15 --------------- crates/stdx/Cargo.toml | 5 +++++ crates/stdx/src/lib.rs | 29 +++++++++++++++++++++-------- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b175ec677e..ae99b966ea1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1197,7 +1197,6 @@ name = "profile" version = "0.0.0" dependencies = [ "arena", - "backtrace", "cfg-if 1.0.0", "libc", "once_cell", @@ -1566,6 +1565,9 @@ dependencies = [ [[package]] name = "stdx" version = "0.0.0" +dependencies = [ + "backtrace", +] [[package]] name = "syn" diff --git a/crates/profile/Cargo.toml b/crates/profile/Cargo.toml index c5dfdff32eb..4951f1835cf 100644 --- a/crates/profile/Cargo.toml +++ b/crates/profile/Cargo.toml @@ -13,7 +13,6 @@ doctest = false once_cell = "1.3.1" cfg-if = "1" libc = "0.2.73" -backtrace = { version = "0.3.44", optional = true } arena = { path = "../arena", version = "0.0.0" } @@ -24,5 +23,4 @@ perf-event = "0.4" cpu_profiler = [] # Uncomment to enable for the whole crate graph -# default = [ "backtrace" ] # default = [ "cpu_profiler" ] diff --git a/crates/profile/src/lib.rs b/crates/profile/src/lib.rs index ab19271c70c..aa6ccc36c3f 100644 --- a/crates/profile/src/lib.rs +++ b/crates/profile/src/lib.rs @@ -15,21 +15,6 @@ stop_watch::{StopWatch, StopWatchSpan}, }; -/// 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: - profile = {{ path = "../profile", features = [ "backtrace"] }} -"# - ); -} - thread_local!(static IN_SCOPE: RefCell = RefCell::new(false)); /// Allows to check if the current code is withing some dynamic scope, can be diff --git a/crates/stdx/Cargo.toml b/crates/stdx/Cargo.toml index 8d7a5115626..c47e8d0a86b 100644 --- a/crates/stdx/Cargo.toml +++ b/crates/stdx/Cargo.toml @@ -10,4 +10,9 @@ edition = "2018" doctest = false [dependencies] +backtrace = { version = "0.3.44", optional = true } # Think twice before adding anything here + +[features] +# Uncomment to enable for the whole crate graph +# default = [ "backtrace" ] diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 13aab1451ec..d9a62e943b6 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -25,6 +25,27 @@ fn drop(&mut self) { 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; -- 2.44.0