diff options
author | Aleksey Kladov <[email protected]> | 2021-01-14 11:55:13 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-01-14 11:55:13 +0000 |
commit | 865e05b5b47d3e27e8fe4458662e751e492a0f51 (patch) | |
tree | af450e09e10f59f3825c775c0d5ee0f3424cf57f /crates/stdx | |
parent | dd5b3cd6f08ece075d1e3ad7c456b1eb7d289d88 (diff) |
Make printin the backtrace more convenient
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/Cargo.toml | 5 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 29 |
2 files changed, 26 insertions, 8 deletions
diff --git a/crates/stdx/Cargo.toml b/crates/stdx/Cargo.toml index 8d7a51156..c47e8d0a8 100644 --- a/crates/stdx/Cargo.toml +++ b/crates/stdx/Cargo.toml | |||
@@ -10,4 +10,9 @@ edition = "2018" | |||
10 | doctest = false | 10 | doctest = false |
11 | 11 | ||
12 | [dependencies] | 12 | [dependencies] |
13 | backtrace = { version = "0.3.44", optional = true } | ||
13 | # Think twice before adding anything here | 14 | # Think twice before adding anything here |
15 | |||
16 | [features] | ||
17 | # Uncomment to enable for the whole crate graph | ||
18 | # default = [ "backtrace" ] | ||
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 { | |||
25 | Guard { label, start: Instant::now() } | 25 | Guard { label, start: Instant::now() } |
26 | } | 26 | } |
27 | 27 | ||
28 | /// Prints backtrace to stderr, useful for debugging. | ||
29 | #[cfg(feature = "backtrace")] | ||
30 | pub fn print_backtrace() { | ||
31 | let bt = backtrace::Backtrace::new(); | ||
32 | eprintln!("{:?}", bt); | ||
33 | } | ||
34 | #[cfg(not(feature = "backtrace"))] | ||
35 | pub fn print_backtrace() { | ||
36 | eprintln!( | ||
37 | r#"Enable the backtrace feature. | ||
38 | Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`. | ||
39 | "# | ||
40 | ); | ||
41 | } | ||
42 | |||
43 | pub fn to_lower_snake_case(s: &str) -> String { | ||
44 | to_snake_case(s, char::to_ascii_lowercase) | ||
45 | } | ||
46 | pub fn to_upper_snake_case(s: &str) -> String { | ||
47 | to_snake_case(s, char::to_ascii_uppercase) | ||
48 | } | ||
28 | fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String { | 49 | fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String { |
29 | let mut buf = String::with_capacity(s.len()); | 50 | let mut buf = String::with_capacity(s.len()); |
30 | let mut prev = false; | 51 | let mut prev = false; |
@@ -43,14 +64,6 @@ fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String { | |||
43 | buf | 64 | buf |
44 | } | 65 | } |
45 | 66 | ||
46 | pub fn to_lower_snake_case(s: &str) -> String { | ||
47 | to_snake_case(s, char::to_ascii_lowercase) | ||
48 | } | ||
49 | |||
50 | pub fn to_upper_snake_case(s: &str) -> String { | ||
51 | to_snake_case(s, char::to_ascii_uppercase) | ||
52 | } | ||
53 | |||
54 | pub fn replace(buf: &mut String, from: char, to: &str) { | 67 | pub fn replace(buf: &mut String, from: char, to: &str) { |
55 | if !buf.contains(from) { | 68 | if !buf.contains(from) { |
56 | return; | 69 | return; |