rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.16k stars 250 forks source link

Remove dependency on cfg_if #536

Closed EriKWDev closed 1 year ago

EriKWDev commented 1 year ago

cfg_if is only used in a single place and seems a bit redundant.

log is such a depended upon library I feel like removing even the smallest dependency is a win

Thomasdezeeuw commented 1 year ago

I don't think if statements are supported in constants yet in the MSRV we use. Other than that I would be 👍

ChrisDenton commented 1 year ago

if, match and loop were const stabilized in 1.46.

EriKWDev commented 1 year ago

Very well. Would something like this be supported then? It should give the exact same result, early returning if a correct one is hit and otherwise continue the "else if {} else {}" chain.

#[rustfmt::skip]
const MAX_LEVEL_INNER: LevelFilter = {
    #[allow(unreachable_code)]
    {
        #[cfg(all(not(debug_assertions), feature = "release_max_level_off"))] {
            return LevelFilter::Off;
        }
        #[cfg(all(not(debug_assertions), feature = "release_max_level_error"))] {
            return LevelFilter::Error;
        }
        #[cfg(all(not(debug_assertions), feature = "release_max_level_warn"))] {
            return LevelFilter::Warn;
        }
        #[cfg(all(not(debug_assertions), feature = "release_max_level_info"))] {
            return LevelFilter::Info;
        }
        #[cfg(all(not(debug_assertions), feature = "release_max_level_debug"))] {
            return LevelFilter::Debug;
        }
        #[cfg(all(not(debug_assertions), feature = "release_max_level_trace"))] {
            return LevelFilter::Trace;
        }
        #[cfg(feature = "max_level_off")] {
            return LevelFilter::Off;
        }
        #[cfg(feature = "max_level_error")] {
            return LevelFilter::Error;
        }
        #[cfg(feature = "max_level_warn")] {
            return LevelFilter::Warn;
        }
        #[cfg(feature = "max_level_info")] {
            return LevelFilter::Info;
        }
        #[cfg(feature = "max_level_debug")] {
            return LevelFilter::Debug;
        }

        LevelFilter::Trace
    }
};

I added the #[allow(unreachable_code)] in case two features were enabled at the same time for there to be no warning, but that is ofc not necessary.

EriKWDev commented 1 year ago

Hmm, apparently no return in const expr. By moving it out to a const fn though we can use the returns :) The above code inside a const fn compiles with 1.31.0 on my end