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
20#[macro_use]
21mod macros;
22
23mod arch;
24
25// This module needs to be public because the `is_{arch}_feature_detected!`
26// macros expand calls to items within it in user crates.
27#[doc(hidden)]
28#[unstable(feature = "stdarch_internal", issue = "none")]
29pub use self::arch::__is_feature_detected;
30pub(crate) use self::arch::Feature;
31
32mod bit;
33mod cache;
34
35cfg_select! {
36    miri => {
37        // When running under miri all target-features that are not enabled at
38        // compile-time are reported as disabled at run-time.
39        //
40        // For features for which `cfg(target_feature)` returns true,
41        // this run-time detection logic is never called.
42        #[path = "os/other.rs"]
43        mod os;
44    }
45    any(target_arch = "x86", target_arch = "x86_64") => {
46        // On x86/x86_64 no OS specific functionality is required.
47        #[path = "os/x86.rs"]
48        mod os;
49    }
50    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    }
57    all(target_os = "freebsd", feature = "libc") => {
58        #[cfg(target_arch = "aarch64")]
59        #[path = "os/aarch64.rs"]
60        mod aarch64;
61        #[path = "os/freebsd/mod.rs"]
62        mod os;
63    }
64    all(target_os = "openbsd", target_arch = "aarch64", feature = "libc") => {
65        #[allow(dead_code)] // we don't use code that calls the mrs instruction.
66        #[path = "os/aarch64.rs"]
67        mod aarch64;
68        #[path = "os/openbsd/aarch64.rs"]
69        mod os;
70    }
71    all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")) => {
72        #[path = "os/windows/aarch64.rs"]
73        mod os;
74    }
75    all(target_vendor = "apple", target_arch = "aarch64", feature = "libc") => {
76        #[path = "os/darwin/aarch64.rs"]
77        mod os;
78    }
79    _ => {
80        #[path = "os/other.rs"]
81        mod os;
82    }
83}
84
85/// Performs run-time feature detection.
86#[inline]
87#[allow(dead_code)]
88fn check_for(x: Feature) -> bool {
89    cache::test(x as u32)
90}
91
92/// Returns an `Iterator<Item=(&'static str, bool)>` where
93/// `Item.0` is the feature name, and `Item.1` is a `bool` which
94/// is `true` if the feature is supported by the host and `false` otherwise.
95#[unstable(feature = "stdarch_internal", issue = "none")]
96pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
97    cfg_select! {
98        any(
99            target_arch = "x86",
100            target_arch = "x86_64",
101            target_arch = "arm",
102            target_arch = "aarch64",
103            target_arch = "arm64ec",
104            target_arch = "riscv32",
105            target_arch = "riscv64",
106            target_arch = "powerpc",
107            target_arch = "powerpc64",
108            target_arch = "mips",
109            target_arch = "mips64",
110            target_arch = "loongarch32",
111            target_arch = "loongarch64",
112            target_arch = "s390x",
113        ) => {
114            (0_u8..Feature::_last as u8).map(|discriminant: u8| {
115                #[allow(bindings_with_variant_name)] // RISC-V has Feature::f
116                let f: Feature = unsafe { core::mem::transmute(discriminant) };
117                let name: &'static str = f.to_str();
118                let enabled: bool = check_for(f);
119                (name, enabled)
120            })
121        }
122        _ => None.into_iter(),
123    }
124}