From cbafd7527d196632cb778b451b5f83ae80762c93 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 13 Apr 2023 07:06:59 +1000 Subject: [PATCH] Remove dep `cfg-if` from tracing (#2553) Same reason as https://github.com/rust-lang/log/pull/536 : `cfg_if` is only used in a single place and `tracing` is used by many other crates, so even removing one dependency will be beneficial. Remove dependency `cfg-if` and replace `cfg_if::cfg_if!` with a `const fn get_max_level_inner() -> LevelFilter` and uses `if cfg!(...)` inside. Using if in const function is stablised in [1.46](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1460-2020-08-27) so this should work fine in msrv 1.56 Signed-off-by: Jiahao XU ; Conflicts: ; tracing/Cargo.toml ; tracing/src/level_filters.rs --- tracing/Cargo.toml | 1 - tracing/src/level_filters.rs | 56 +++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 50bedb07fa..7cf472048b 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -31,7 +31,6 @@ rust-version = "1.56.0" tracing-core = { path = "../tracing-core", version = "0.1.30", default-features = false } log = { version = "0.4.17", optional = true } tracing-attributes = { path = "../tracing-attributes", version = "0.1.23", optional = true } -cfg-if = "1.0.0" pin-project-lite = "0.2.9" [dev-dependencies] diff --git a/tracing/src/level_filters.rs b/tracing/src/level_filters.rs index 44f5e5f57a..4e56ada2c5 100644 --- a/tracing/src/level_filters.rs +++ b/tracing/src/level_filters.rs @@ -62,33 +62,37 @@ pub use tracing_core::{metadata::ParseLevelFilterError, LevelFilter}; /// `Span` constructors should compare the level against this value to /// determine if those spans or events are enabled. /// -/// [module-level documentation]: super#compile-time-filters -pub const STATIC_MAX_LEVEL: LevelFilter = MAX_LEVEL; +/// [module-level documentation]: self#compile-time-filters +pub const STATIC_MAX_LEVEL: LevelFilter = get_max_level_inner(); -cfg_if::cfg_if! { - if #[cfg(all(not(debug_assertions), feature = "release_max_level_off"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::OFF; - } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_error"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::ERROR; - } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_warn"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::WARN; - } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_info"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::INFO; - } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_debug"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::DEBUG; - } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_trace"))] { - const MAX_LEVEL: LevelFilter = LevelFilter::TRACE; - } else if #[cfg(feature = "max_level_off")] { - const MAX_LEVEL: LevelFilter = LevelFilter::OFF; - } else if #[cfg(feature = "max_level_error")] { - const MAX_LEVEL: LevelFilter = LevelFilter::ERROR; - } else if #[cfg(feature = "max_level_warn")] { - const MAX_LEVEL: LevelFilter = LevelFilter::WARN; - } else if #[cfg(feature = "max_level_info")] { - const MAX_LEVEL: LevelFilter = LevelFilter::INFO; - } else if #[cfg(feature = "max_level_debug")] { - const MAX_LEVEL: LevelFilter = LevelFilter::DEBUG; +const fn get_max_level_inner() -> LevelFilter { + if cfg!(not(debug_assertions)) { + if cfg!(feature = "release_max_level_off") { + LevelFilter::OFF + } else if cfg!(feature = "release_max_level_error") { + LevelFilter::ERROR + } else if cfg!(feature = "release_max_level_warn") { + LevelFilter::WARN + } else if cfg!(feature = "release_max_level_info") { + LevelFilter::INFO + } else if cfg!(feature = "release_max_level_debug") { + LevelFilter::DEBUG + } else { + // Same as branch cfg!(feature = "release_max_level_trace") + LevelFilter::TRACE + } + } else if cfg!(feature = "max_level_off") { + LevelFilter::OFF + } else if cfg!(feature = "max_level_error") { + LevelFilter::ERROR + } else if cfg!(feature = "max_level_warn") { + LevelFilter::WARN + } else if cfg!(feature = "max_level_info") { + LevelFilter::INFO + } else if cfg!(feature = "max_level_debug") { + LevelFilter::DEBUG } else { - const MAX_LEVEL: LevelFilter = LevelFilter::TRACE; + // Same as branch cfg!(feature = "max_level_trace") + LevelFilter::TRACE } }