std_detect/detect/
mod.rs

1//! This module implements run-time feature detection.
2//!
3//! The `is_{arch}_feature_detected!("feature-name")` macros take the name of a
4//! feature as a string-literal, and return a boolean indicating whether the
5//! feature is enabled at run-time or not.
6//!
7//! These macros do two things:
8//! * map the string-literal into an integer stored as a `Feature` enum,
9//! * call a `os::check_for(x: Feature)` function that returns `true` if the
10//! feature is enabled.
11//!
12//! The `Feature` enums are also implemented in the `arch/{target_arch}.rs`
13//! modules.
14//!
15//! The `check_for` functions are, in general, Operating System dependent. Most
16//! architectures do not allow user-space programs to query the feature bits
17//! due to security concerns (x86 is the big exception). These functions are
18//! implemented in the `os/{target_os}.rs` modules.
19
20use cfg_if::cfg_if;
21
22#[macro_use]
23mod macros;
24
25mod arch;
26
27// This module needs to be public because the `is_{arch}_feature_detected!`
28// macros expand calls to items within it in user crates.
29#[doc(hidden)]
30#[unstable(feature = "stdarch_internal", issue = "none")]
31pub use self::arch::__is_feature_detected;
32pub(crate) use self::arch::Feature;
33
34mod bit;
35mod cache;
36
37cfg_if! {
38    if #[cfg(miri)] {
39        // When running under miri all target-features that are not enabled at
40        // compile-time are reported as disabled at run-time.
41        //
42        // For features for which `cfg(target_feature)` returns true,
43        // this run-time detection logic is never called.
44        #[path = "os/other.rs"]
45        mod os;
46    } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
47        // On x86/x86_64 no OS specific functionality is required.
48        #[path = "os/x86.rs"]
49        mod os;
50    } else if #[cfg(all(any(target_os = "linux", target_os = "android"), feature = "libc"))] {
51        #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
52        #[path = "os/riscv.rs"]
53        mod riscv;
54        #[path = "os/linux/mod.rs"]
55        mod os;
56    } else if #[cfg(all(target_os = "freebsd", feature = "libc"))] {
57        #[cfg(target_arch = "aarch64")]
58        #[path = "os/aarch64.rs"]
59        mod aarch64;
60        #[path = "os/freebsd/mod.rs"]
61        mod os;
62    } else if #[cfg(all(target_os = "openbsd", target_arch = "aarch64", feature = "libc"))] {
63        #[allow(dead_code)] // we don't use code that calls the mrs instruction.
64        #[path = "os/aarch64.rs"]
65        mod aarch64;
66        #[path = "os/openbsd/aarch64.rs"]
67        mod os;
68    } else if #[cfg(all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")))] {
69        #[path = "os/windows/aarch64.rs"]
70        mod os;
71    } else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64", feature = "libc"))] {
72        #[path = "os/darwin/aarch64.rs"]
73        mod os;
74    } else {
75        #[path = "os/other.rs"]
76        mod os;
77    }
78}
79
80/// Performs run-time feature detection.
81#[inline]
82#[allow(dead_code)]
83fn check_for(x: Feature) -> bool {
84    cache::test(x as u32)
85}
86
87/// Returns an `Iterator<Item=(&'static str, bool)>` where
88/// `Item.0` is the feature name, and `Item.1` is a `bool` which
89/// is `true` if the feature is supported by the host and `false` otherwise.
90#[unstable(feature = "stdarch_internal", issue = "none")]
91pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
92    cfg_if! {
93        if #[cfg(any(
94            target_arch = "x86",
95            target_arch = "x86_64",
96            target_arch = "arm",
97            target_arch = "aarch64",
98            target_arch = "arm64ec",
99            target_arch = "riscv32",
100            target_arch = "riscv64",
101            target_arch = "powerpc",
102            target_arch = "powerpc64",
103            target_arch = "mips",
104            target_arch = "mips64",
105            target_arch = "loongarch32",
106            target_arch = "loongarch64",
107            target_arch = "s390x",
108        ))] {
109            (0_u8..Feature::_last as u8).map(|discriminant: u8| {
110                #[allow(bindings_with_variant_name)] // RISC-V has Feature::f
111                let f: Feature = unsafe { core::mem::transmute(discriminant) };
112                let name: &'static str = f.to_str();
113                let enabled: bool = check_for(f);
114                (name, enabled)
115            })
116        } else {
117            None.into_iter()
118        }
119    }
120}