I am trying to set value of constant based on target_os
. I want to achieve something like this:
#[cfg(target_os = "linux")]
const MAP_FLAGS: libc::c_int = libc::MAP_POPULATE;
#[cfg(target_os = "macos")]
const MAP_FLAGS: libc::c_int = libc::MAP_NOCACHE;
#[cfg(/* ELSE */)]
const MAP_FLAGS: libc::c_int = 0;
Now, natural idea is to use cfg!
macro, but that won't compile because libc::MAP_POPULATE
is present only on linux targets and libc::MAP_NOCACHE
is present only on macos targets.
Any idea how to solve this?
I think you're very close. If you want a default just for anything that's not linux or macos, you can combine not()
with any()
:
#[cfg(target_os = "linux")]
const MAP_FLAGS: libc::c_int = libc::MAP_POPULATE;
#[cfg(target_os = "macos")]
const MAP_FLAGS: libc::c_int = libc::MAP_NOCACHE;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
const MAP_FLAGS: libc::c_int = 0;