RFC: cfg_os_version_min by ChrisDenton · Pull Request #3750 · rust-lang/rfcs (original) (raw)
To further the bikeshedding: os_version_min
is definitely wrong, it should at the very least be version_min
(since it works for libc
too, which is not an OS).
But how about the name available
?
exftern "C" { #[cfg(any( available("libc", "x.y.z"), available("macos", "10.12"), ))] fn foo(); }
if cfg!(available("windows", "10.0.10240")) { // ... }
A nice thing about available
is that it reads a tiny bit more like English:
#[cfg(version_min("macos", "10.12"))] // configured where version minimum macOS 10.12 #[cfg(available("macos", "10.12"))] // configured where available macOS 10.12.
While still avoiding the "does cfg!(xyz("macos", "10.12"))
mean >10.12 or >=10.12" issue.
This could also tie in nicely with a macro available!
that first does the static check, and then falls back to a runtime version check against e.g. gnu_get_libc_version()
(not proposing this macro here, and probably not implement-able everywhere either, but just to get the idea across):
if available!("libc", "x.y.z") || available!("macos", "10.12") {
// ... Dynamically use some new feature or API, maybe with dlsym
or libloading.
}
(Note: I'm heavily biased by Swift's @available).