rust-lang / cfg-if

A if/elif-like macro for Rust #[cfg] statements
Apache License 2.0
557 stars 40 forks source link

Nested if #6

Closed peng1999 closed 5 years ago

peng1999 commented 6 years ago

Allowing nested if-statment can reduce indent level:

cfg_if! {
    if #[cfg(windows)] {
        mod windows_only;

        if #[test] {
            mod test;
        }
    }
}

Is that reasonable?

alexcrichton commented 6 years ago

Perhaps! I have no idea how to structure the macro to do that, but if it can be done then it could perhaps be done

jwir3 commented 5 years ago

This would be really useful for me, where I'm wanting to do something like:

cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
        // When the `console_error_panic_hook` feature is enabled, we can call the
        // `set_panic_hook` function to get better error messages if we ever panic.
        if #[cfg(feature = "console_error_panic_hook")] {
            extern crate console_error_panic_hook;
            use console_error_panic_hook::set_once as set_panic_hook;
        } else {
            #[inline]
            fn set_panic_hook() {}
        }
    }
}
alexcrichton commented 5 years ago

I'm going to close this because, while convenient, I don't think it's very easily supported in the macro and would prefer to not overly complicate the macro too much. If others want to maintain this on crates.io, though, that's totally fine!