Skip to content

Commit f3da046

Browse files
committed
Override the features detected using an env::var
Fixes: #804
1 parent 9fe42b0 commit f3da046

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

crates/std_detect/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ auxv = "0.3.3"
3131
cupid = "0.6.0"
3232

3333
[features]
34-
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io" ]
34+
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io", "std_detect_env_override" ]
3535
std_detect_file_io = []
36-
std_detect_dlsym_getauxval = [ "libc" ]
36+
std_detect_dlsym_getauxval = [ "libc" ]
37+
std_detect_env_override = []

crates/std_detect/src/detect/cache.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ impl Cache {
162162
self.1.store(hi, Ordering::Relaxed);
163163
}
164164
}
165+
cfg_if! {
166+
if #[cfg(feature = "std_detect_env_override")] {
167+
fn env_override(mut value: Initializer) -> Initializer {
168+
if let Ok(disable) = crate::env::var("RUST_STD_DETECT_UNSTABLE") {
169+
for v in disable.split(" ") {
170+
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
171+
}
172+
value
173+
} else {
174+
value
175+
}
176+
}
177+
} else {
178+
#[inline]
179+
fn env_override(value: Initializer) -> Initializer {
180+
value
181+
}
182+
}
183+
}
184+
165185

166186
/// Tests the `bit` of the storage. If the storage has not been initialized,
167187
/// initializes it with the result of `f()`.
@@ -171,13 +191,17 @@ impl Cache {
171191
///
172192
/// It uses the `Feature` variant to index into this variable as a bitset. If
173193
/// the bit is set, the feature is enabled, and otherwise it is disabled.
194+
///
195+
/// If the feature `std_detect_env_override` is enabled looks for the env
196+
/// variable `RUST_STD_DETECT_UNSTABLE` and uses its its content to disable
197+
/// Features that would had been otherwise detected.
174198
#[inline]
175199
pub(crate) fn test<F>(bit: u32, f: F) -> bool
176200
where
177201
F: FnOnce() -> Initializer,
178202
{
179203
if CACHE.is_uninitialized() {
180-
CACHE.initialize(f());
204+
CACHE.initialize(env_override(f()));
181205
}
182206
CACHE.test(bit)
183207
}

crates/std_detect/src/detect/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ macro_rules! features {
6969
Feature::_last => unreachable!(),
7070
}
7171
}
72+
pub fn from_str(s: &str) -> Result<Feature, ()> {
73+
match s {
74+
$($feature_lit => Ok(Feature::$feature),)*
75+
_ => Err(())
76+
}
77+
}
7278
}
7379

7480
/// Each function performs run-time feature detection for a single

crates/std_detect/src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@
2424
extern crate cfg_if;
2525

2626
cfg_if! {
27-
if #[cfg(feature = "std_detect_file_io")] {
27+
if #[cfg(all(feature = "std_detect_file_io", feature = "std_detect_env_override"))] {
28+
#[cfg_attr(test, macro_use(println))]
29+
extern crate std;
30+
31+
#[allow(unused_imports)]
32+
use std::{arch, env, fs, io, mem, sync};
33+
} else if #[cfg(feature = "std_detect_file_io")] {
2834
#[cfg_attr(test, macro_use(println))]
2935
extern crate std;
3036

3137
#[allow(unused_imports)]
3238
use std::{arch, fs, io, mem, sync};
39+
} else if #[cfg(feature = "std_detect_env_override")] {
40+
#[cfg_attr(test, macro_use(println))]
41+
extern crate std;
42+
43+
use std::env;
3344
} else {
3445
#[cfg(test)]
3546
#[macro_use(println)]

0 commit comments

Comments
 (0)