diff options
| author | Luca Barbato <lu_zero@gentoo.org> | 2019-09-17 15:36:23 +0200 |
|---|---|---|
| committer | Luca Barbato <lu_zero@gentoo.org> | 2019-09-17 19:22:18 +0200 |
| commit | 6420fa4fb07a7aeab5fc57480bb6c3d619505539 (patch) | |
| tree | 36598c3abbae975beb52da3f07cb24c7c2e68f6d /library/stdarch/crates/std_detect/src | |
| parent | 1855195f402e1873ca40eaae365e20104bad866d (diff) | |
| download | rust-6420fa4fb07a7aeab5fc57480bb6c3d619505539.tar.gz rust-6420fa4fb07a7aeab5fc57480bb6c3d619505539.zip | |
Override the features detected using an env::var
Fixes: #804
Diffstat (limited to 'library/stdarch/crates/std_detect/src')
| -rw-r--r-- | library/stdarch/crates/std_detect/src/detect/cache.rs | 25 | ||||
| -rw-r--r-- | library/stdarch/crates/std_detect/src/detect/macros.rs | 6 | ||||
| -rw-r--r-- | library/stdarch/crates/std_detect/src/lib.rs | 13 |
3 files changed, 42 insertions, 2 deletions
diff --git a/library/stdarch/crates/std_detect/src/detect/cache.rs b/library/stdarch/crates/std_detect/src/detect/cache.rs index 0056cd3026f..1de4316b8f4 100644 --- a/library/stdarch/crates/std_detect/src/detect/cache.rs +++ b/library/stdarch/crates/std_detect/src/detect/cache.rs @@ -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()`. @@ -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) } diff --git a/library/stdarch/crates/std_detect/src/detect/macros.rs b/library/stdarch/crates/std_detect/src/detect/macros.rs index 750cf50aa70..c9200d2f1f1 100644 --- a/library/stdarch/crates/std_detect/src/detect/macros.rs +++ b/library/stdarch/crates/std_detect/src/detect/macros.rs @@ -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 diff --git a/library/stdarch/crates/std_detect/src/lib.rs b/library/stdarch/crates/std_detect/src/lib.rs index 7737719c3b0..9f4b9918e7a 100644 --- a/library/stdarch/crates/std_detect/src/lib.rs +++ b/library/stdarch/crates/std_detect/src/lib.rs @@ -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)] |
