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", feature = "libc") => {
65        #[allow(dead_code)] // we don't use code that calls the mrs instruction.
66        #[cfg(target_arch = "aarch64")]
67        #[path = "os/aarch64.rs"]
68        mod aarch64;
69        #[path = "os/openbsd/mod.rs"]
70        mod os;
71    }
72    all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")) => {
73        #[path = "os/windows/aarch64.rs"]
74        mod os;
75    }
76    all(target_vendor = "apple", target_arch = "aarch64", feature = "libc") => {
77        #[path = "os/darwin/aarch64.rs"]
78        mod os;
79    }
80    _ => {
81        #[path = "os/other.rs"]
82        mod os;
83    }
84}
85
86/// Performs run-time feature detection.
87#[inline]
88#[allow(dead_code)]
89fn check_for(x: Feature) -> bool {
90    cache::test(x as u32)
91}
92
93/// Returns an `Iterator<Item=(&'static str, bool)>` where
94/// `Item.0` is the feature name, and `Item.1` is a `bool` which
95/// is `true` if the feature is supported by the host and `false` otherwise.
96#[unstable(feature = "stdarch_internal", issue = "none")]
97pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
98    cfg_select! {
99        any(
100            target_arch = "x86",
101            target_arch = "x86_64",
102            target_arch = "arm",
103            target_arch = "aarch64",
104            target_arch = "arm64ec",
105            target_arch = "riscv32",
106            target_arch = "riscv64",
107            target_arch = "powerpc",
108            target_arch = "powerpc64",
109            target_arch = "mips",
110            target_arch = "mips64",
111            target_arch = "loongarch32",
112            target_arch = "loongarch64",
113            target_arch = "s390x",
114        ) => {
115            (0_u8..Feature::_last as u8).map(|discriminant: u8| {
116                #[allow(bindings_with_variant_name)] // RISC-V has Feature::f
117                let f: Feature = unsafe { core::mem::transmute(discriminant) };
118                let name: &'static str = f.to_str();
119                let enabled: bool = check_for(f);
120                (name, enabled)
121            })
122        }
123        _ => None.into_iter(),
124    }
125}