Skip to content

Commit

Permalink
Override the features detected using an env::var
Browse files Browse the repository at this point in the history
Fixes: #804
  • Loading branch information
lu-zero committed Sep 17, 2019
1 parent 9fe42b0 commit b8aecbf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
5 changes: 3 additions & 2 deletions crates/std_detect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ auxv = "0.3.3"
cupid = "0.6.0"

[features]
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io" ]
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io", "std_detect_env_override" ]
std_detect_file_io = []
std_detect_dlsym_getauxval = [ "libc" ]
std_detect_dlsym_getauxval = [ "libc" ]
std_detect_env_override = []
25 changes: 24 additions & 1 deletion crates/std_detect/src/detect/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ impl Cache {
self.1.store(hi, Ordering::Relaxed);
}
}
cfg_if! {
if #[cfg(feature = "std_detect_env_override")] {
fn env_override(mut value: Initializer) -> Initializer {
if let Ok(disable) = crate::env::var("RUST_STD_DETECT_UNSTABLE") {
for v in disable.split(" ") {
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
}
value
} else {
value
}
}
} else {
#[inline]
fn env_override(value: Initializer) -> Initializer {
value
}
}
}

/// Tests the `bit` of the storage. If the storage has not been initialized,
/// initializes it with the result of `f()`.
Expand All @@ -171,13 +190,17 @@ impl Cache {
///
/// It uses the `Feature` variant to index into this variable as a bitset. If
/// the bit is set, the feature is enabled, and otherwise it is disabled.
///
/// If the feature `std_detect_env_override` is enabled looks for the env
/// variable `RUST_STD_DETECT_UNSTABLE` and uses its its content to disable
/// Features that would had been otherwise detected.
#[inline]
pub(crate) fn test<F>(bit: u32, f: F) -> bool
where
F: FnOnce() -> Initializer,
{
if CACHE.is_uninitialized() {
CACHE.initialize(f());
CACHE.initialize(env_override(f()));
}
CACHE.test(bit)
}
6 changes: 6 additions & 0 deletions crates/std_detect/src/detect/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ macro_rules! features {
Feature::_last => unreachable!(),
}
}
pub fn from_str(s: &str) -> Result<Feature, ()> {
match s {
$($feature_lit => Ok(Feature::$feature),)*
_ => Err(())
}
}
}

/// Each function performs run-time feature detection for a single
Expand Down
13 changes: 12 additions & 1 deletion crates/std_detect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@
extern crate cfg_if;

cfg_if! {
if #[cfg(feature = "std_detect_file_io")] {
if #[cfg(all(feature = "std_detect_file_io", feature = "std_detect_env_override"))] {
#[cfg_attr(test, macro_use(println))]
extern crate std;

#[allow(unused_imports)]
use std::{arch, env, fs, io, mem, sync};
} else if #[cfg(feature = "std_detect_file_io")] {
#[cfg_attr(test, macro_use(println))]
extern crate std;

#[allow(unused_imports)]
use std::{arch, fs, io, mem, sync};
} else if #[cfg(feature = "std_detect_env_override")] {
#[cfg_attr(test, macro_use(println))]
extern crate std;

use std::env;
} else {
#[cfg(test)]
#[macro_use(println)]
Expand Down

0 comments on commit b8aecbf

Please sign in to comment.