Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to conditionally compile based on the existence of a constant?

The libc library defines a number of constants that can be passed as flags to open() such as O_APPEND, O_CREAT, etc. Some of these flags are only defined for certain OS targets. It'd be nice if there was a way to freely use O_WHATEVER in my code without worrying about whether it was actually defined in libc:

#[cfg(???)]
const O_WHATEVER = libc::O_WHATEVER;
#[cfg(not(???))]
const O_WHATEVER = 0;

It'd be nice if the ??? in my example above was some kind of is_defined(libc::O_WHATEVER). Otherwise, I'd need to figure out all of the OS combinations in which O_WHATEVER is defined in libc and that'd be a real pain.

like image 249
bmatcuk Avatar asked Nov 18 '25 09:11

bmatcuk


1 Answers

Not as of Rust 1.41. RFC 2523 — cfg_version and cfg_accessible introduces:

#[cfg(accessible(libc::O_WHATEVER))]
const O_WHATEVER = libc::O_WHATEVER;
like image 173
Shepmaster Avatar answered Nov 20 '25 01:11

Shepmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!