Skip to content

Commit

Permalink
Factor out check_for
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lu-zero authored and gnzlbg committed Sep 9, 2019
1 parent e0ab2c1 commit 9fad964
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 63 deletions.
7 changes: 6 additions & 1 deletion crates/std_detect/src/detect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ cfg_if! {
mod os;
}
}
pub use self::os::check_for;

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, self::os::detect_features)
}
9 changes: 1 addition & 8 deletions crates/std_detect/src/detect/os/freebsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
//! Run-time feature detection for Aarch64 on FreeBSD.

use crate::detect::{Feature, cache};
use super::super::aarch64::detect_features;

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}
pub use super::super::aarch64::detect_features;

#[cfg(test)]
mod tests {
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/freebsd/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
use crate::detect::{Feature, cache};
use super::{auxvec};

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Try to read the features from the auxiliary vector
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();
let enable_feature = |value: &mut cache::Initializer, f, enable| {
if enable {
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/freebsd/powerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
use crate::detect::{Feature, cache};
use super::{auxvec};

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();
let enable_feature = |value: &mut cache::Initializer, f, enable| {
if enable {
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/linux/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
use crate::detect::{Feature, cache, bit};
use super::{auxvec, cpuinfo};

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Try to read the features from the auxiliary vector, and if that fails, try
/// to read them from /proc/cpuinfo.
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
if let Ok(auxv) = auxvec::auxv() {
let hwcap: AtHwcap = auxv.into();
return hwcap.cache();
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/linux/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
use crate::detect::{Feature, cache, bit};
use super::{auxvec, cpuinfo};

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Try to read the features from the auxiliary vector, and if that fails, try
/// to read them from /proc/cpuinfo.
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();
let enable_feature = |value: &mut cache::Initializer, f, enable| {
if enable {
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/linux/mips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
use crate::detect::{Feature, cache, bit};
use super::auxvec;

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Try to read the features from the auxiliary vector, and if that fails, try
/// to read them from `/proc/cpuinfo`.
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();
let enable_feature = |value: &mut cache::Initializer, f, enable| {
if enable {
Expand Down
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/linux/powerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
use crate::detect::{Feature, cache};
use super::{auxvec, cpuinfo};

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Try to read the features from the auxiliary vector, and if that fails, try
/// to read them from /proc/cpuinfo.
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();
let enable_feature = |value: &mut cache::Initializer, f, enable| {
if enable {
Expand Down
8 changes: 3 additions & 5 deletions crates/std_detect/src/detect/os/other.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Other operating systems

use crate::detect::Feature;
use crate::detect::cache;

/// Performs run-time feature detection.
#[inline]
pub fn check_for(_x: Feature) -> bool {
false
pub(crate) fn detect_features() -> cache::Initializer {
cache::Initializer::default()
}
8 changes: 1 addition & 7 deletions crates/std_detect/src/detect/os/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ use crate::mem;

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

/// Performs run-time feature detection.
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}

/// Run-time feature detection on x86 works by using the CPUID instruction.
///
/// The [CPUID Wikipedia page][wiki_cpuid] contains
Expand All @@ -31,7 +25,7 @@ pub fn check_for(x: Feature) -> bool {
/// [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
/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf
#[allow(clippy::similar_names)]
fn detect_features() -> cache::Initializer {
pub(crate) fn detect_features() -> cache::Initializer {
let mut value = cache::Initializer::default();

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

0 comments on commit 9fad964

Please sign in to comment.