Skip to content

Commit 9fad964

Browse files
lu-zerognzlbg
authored andcommitted
Factor out check_for
All the os-specific code implements a `check_for` and a `detect_features`. Move the always identical check_for in the mod.rs and use `os::detect_features` there.
1 parent e0ab2c1 commit 9fad964

File tree

10 files changed

+17
-63
lines changed

10 files changed

+17
-63
lines changed

crates/std_detect/src/detect/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ cfg_if! {
9090
mod os;
9191
}
9292
}
93-
pub use self::os::check_for;
93+
94+
/// Performs run-time feature detection.
95+
#[inline]
96+
pub fn check_for(x: Feature) -> bool {
97+
cache::test(x as u32, self::os::detect_features)
98+
}

crates/std_detect/src/detect/os/freebsd/aarch64.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
//! Run-time feature detection for Aarch64 on FreeBSD.
22
3-
use crate::detect::{Feature, cache};
4-
use super::super::aarch64::detect_features;
5-
6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
3+
pub use super::super::aarch64::detect_features;
114

125
#[cfg(test)]
136
mod tests {

crates/std_detect/src/detect/os/freebsd/arm.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
use crate::detect::{Feature, cache};
44
use super::{auxvec};
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
126
/// Try to read the features from the auxiliary vector
13-
fn detect_features() -> cache::Initializer {
7+
pub(crate) fn detect_features() -> cache::Initializer {
148
let mut value = cache::Initializer::default();
159
let enable_feature = |value: &mut cache::Initializer, f, enable| {
1610
if enable {

crates/std_detect/src/detect/os/freebsd/powerpc.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
use crate::detect::{Feature, cache};
44
use super::{auxvec};
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
12-
fn detect_features() -> cache::Initializer {
6+
pub(crate) fn detect_features() -> cache::Initializer {
137
let mut value = cache::Initializer::default();
148
let enable_feature = |value: &mut cache::Initializer, f, enable| {
159
if enable {

crates/std_detect/src/detect/os/linux/aarch64.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
use crate::detect::{Feature, cache, bit};
44
use super::{auxvec, cpuinfo};
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
126
/// Try to read the features from the auxiliary vector, and if that fails, try
137
/// to read them from /proc/cpuinfo.
14-
fn detect_features() -> cache::Initializer {
8+
pub(crate) fn detect_features() -> cache::Initializer {
159
if let Ok(auxv) = auxvec::auxv() {
1610
let hwcap: AtHwcap = auxv.into();
1711
return hwcap.cache();

crates/std_detect/src/detect/os/linux/arm.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
use crate::detect::{Feature, cache, bit};
44
use super::{auxvec, cpuinfo};
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
126
/// Try to read the features from the auxiliary vector, and if that fails, try
137
/// to read them from /proc/cpuinfo.
14-
fn detect_features() -> cache::Initializer {
8+
pub(crate) fn detect_features() -> cache::Initializer {
159
let mut value = cache::Initializer::default();
1610
let enable_feature = |value: &mut cache::Initializer, f, enable| {
1711
if enable {

crates/std_detect/src/detect/os/linux/mips.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
use crate::detect::{Feature, cache, bit};
44
use super::auxvec;
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
126
/// Try to read the features from the auxiliary vector, and if that fails, try
137
/// to read them from `/proc/cpuinfo`.
14-
fn detect_features() -> cache::Initializer {
8+
pub(crate) fn detect_features() -> cache::Initializer {
159
let mut value = cache::Initializer::default();
1610
let enable_feature = |value: &mut cache::Initializer, f, enable| {
1711
if enable {

crates/std_detect/src/detect/os/linux/powerpc.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
use crate::detect::{Feature, cache};
44
use super::{auxvec, cpuinfo};
55

6-
/// Performs run-time feature detection.
7-
#[inline]
8-
pub fn check_for(x: Feature) -> bool {
9-
cache::test(x as u32, detect_features)
10-
}
11-
126
/// Try to read the features from the auxiliary vector, and if that fails, try
137
/// to read them from /proc/cpuinfo.
14-
fn detect_features() -> cache::Initializer {
8+
pub(crate) fn detect_features() -> cache::Initializer {
159
let mut value = cache::Initializer::default();
1610
let enable_feature = |value: &mut cache::Initializer, f, enable| {
1711
if enable {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Other operating systems
22
3-
use crate::detect::Feature;
3+
use crate::detect::cache;
44

5-
/// Performs run-time feature detection.
6-
#[inline]
7-
pub fn check_for(_x: Feature) -> bool {
8-
false
5+
pub(crate) fn detect_features() -> cache::Initializer {
6+
cache::Initializer::default()
97
}

crates/std_detect/src/detect/os/x86.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ use crate::mem;
99

1010
use crate::detect::{Feature, cache, bit};
1111

12-
/// Performs run-time feature detection.
13-
#[inline]
14-
pub fn check_for(x: Feature) -> bool {
15-
cache::test(x as u32, detect_features)
16-
}
17-
1812
/// Run-time feature detection on x86 works by using the CPUID instruction.
1913
///
2014
/// The [CPUID Wikipedia page][wiki_cpuid] contains
@@ -31,7 +25,7 @@ pub fn check_for(x: Feature) -> bool {
3125
/// [intel64_ref]: http://www.intel.de/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
3226
/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf
3327
#[allow(clippy::similar_names)]
34-
fn detect_features() -> cache::Initializer {
28+
pub(crate) fn detect_features() -> cache::Initializer {
3529
let mut value = cache::Initializer::default();
3630

3731
// If the x86 CPU does not support the CPUID instruction then it is too

0 commit comments

Comments
 (0)