I've been doing some work on gfx
and surfman
lately which, due to the nature of providing cross-platform APIs with platform specific backends, can have lots of #[cfg]
checks. You would wish that these could always be simple and apparent such as #[cfg(windows)]
or #[cfg(feature = "x11")]
, but often times you need extra constraints such as:
#[cfg(all(any(feature = "sm-x11", all(unix, not(any(target_os = "macos", target_os = "android"))))))]
Now the meaning of these long checks are often simple, and the problem is that when you need to do something like that somewhere you could easily need to make the same check in related code all over the place. Obviously this isn't always the case, but it got me thinking about maybe being able to provide cfg
aliases. Essentially you would just say:
should_have_x11=[cfg(all(any(feature = "sm-x11", all(unix, not(any(target_os = "macos", target_os = "android"))))))
Then anywhere you need to make that check you just do:
#[cfg(should_have_x11)]
This is much cleaner and makes it tons easier to make sure you are properly indicating the intent of the cfg check. Granted it also arguably adds a level of indirection that makes the developer not instantly able to tell when that code would be included, but I think that it would be worth it in situations and that the developer could make the call.
I guess this is something more for the rust internals forum or a pre-RFC or something like that, but I wanted to post here first to make sure there isn't anything similar already.
Now that I say that, I guess I could make a macro that does this. That wouldn't even need a compiler change so that is sounding very appealing, actually, as long as there isn't a major blocker for where you are allowed to expand syntax with a macro, which there might be depending on how #[cfg]
like you want it.